如何使用Java分割json文件中的数据?

How to split the data in json file using java?

本问题已经有最佳答案,请猛点这里访问。

我有一个json文件,其中包含很多我想分开的字段,以便可以将其用作每个不同的字段。基本上我想将它们拆分并将它们存储为每个单独的字段,以便可以在mysql中使用该文件。 Json File Snippet


看起来这是逐行JSONObjects(无数组)。 逐行读取文件,并使用该行创建一个JSONObject。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
  JSONObject json = new JSONObject(line);
  //json.getString("Slaves");
  //json.getStrng("Job Reader");

//and so on..

//if you want to get all avilable keys.. use
    Iterator< ? > keys = json.keys();

    while( keys.hasNext() ){
        String key = (String)keys.next();
        if( json.get(key) instanceof JSONObject ){
            //key = the key and json.get(key) the value.
        }
    }
}
br.close();