在一行中拆分多个 JSON 字符串
Splitt several JSON Strings in a single Line
我有一个 txt 文件,它是包含几个 JSON 字符串的单行。我的问题是我不知道如何获取每个 JSON 对象。
try {
FileReader fr = new FileReader("SelectedChoice.txt");
BufferedReader br = new BufferedReader(fr);
String zeile ="";
while((zeile = br.readLine())!=null) {
System.out.println(zeile);
JSONObject choice = new JSONObject(zeile);
System.out.println(choice);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这是需要转换的字符串:
{"Item":"MasterNode","Choice 1":1,"Choice 2":0,"Choice 3":-1}{"Item":"WorkerNode","Choice 1":1,"Choice 2":0,"Choice 3":-1}
此代码仅转换行中的第一个 JSON 字符串,而我想转换所有字符串。
所以我在我的 IDE 中提取它并下载了 org.json
库并且它 运行 很好。由于您读取文件的方式,您可能会丢失一行而只保留最后一行。如果你想保存 JSONObject
,你总是可以尝试将 JSONObject
保存在 ArrayList
或其他一些 collection 中,例如这个...
Collection<JSONObject> JSONObjects = new ArrayList<>();
try {
FileReader fr = new FileReader("SelectedChoice.txt");
BufferedReader br = new BufferedReader(fr);
String zeile ="";
while((zeile = br.readLine())!=null) {
System.out.println(zeile);
JSONObject choice = new JSONObject(zeile);
System.out.println(choice);
JSONObjects.add(choice);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
您可以在文件读取完毕后访问 Collection。
希望对您有所帮助!祝你好运!
P.S.如有其他问题欢迎随时提问!
Edit
So I just read in the comments above that you thought it should be one line. If >you want the too groups to be separate, you might want to put the new line >back. If you want that to be one JSONObject
, you might be running into a >syntax error. Try adding a ,
in between the two groups of values.
我有一个 txt 文件,它是包含几个 JSON 字符串的单行。我的问题是我不知道如何获取每个 JSON 对象。
try {
FileReader fr = new FileReader("SelectedChoice.txt");
BufferedReader br = new BufferedReader(fr);
String zeile ="";
while((zeile = br.readLine())!=null) {
System.out.println(zeile);
JSONObject choice = new JSONObject(zeile);
System.out.println(choice);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这是需要转换的字符串:
{"Item":"MasterNode","Choice 1":1,"Choice 2":0,"Choice 3":-1}{"Item":"WorkerNode","Choice 1":1,"Choice 2":0,"Choice 3":-1}
此代码仅转换行中的第一个 JSON 字符串,而我想转换所有字符串。
所以我在我的 IDE 中提取它并下载了 org.json
库并且它 运行 很好。由于您读取文件的方式,您可能会丢失一行而只保留最后一行。如果你想保存 JSONObject
,你总是可以尝试将 JSONObject
保存在 ArrayList
或其他一些 collection 中,例如这个...
Collection<JSONObject> JSONObjects = new ArrayList<>();
try {
FileReader fr = new FileReader("SelectedChoice.txt");
BufferedReader br = new BufferedReader(fr);
String zeile ="";
while((zeile = br.readLine())!=null) {
System.out.println(zeile);
JSONObject choice = new JSONObject(zeile);
System.out.println(choice);
JSONObjects.add(choice);
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
您可以在文件读取完毕后访问 Collection。
希望对您有所帮助!祝你好运!
P.S.如有其他问题欢迎随时提问!
Edit
So I just read in the comments above that you thought it should be one line. If >you want the too groups to be separate, you might want to put the new line >back. If you want that to be one
JSONObject
, you might be running into a >syntax error. Try adding a,
in between the two groups of values.