我使用JSON时遇到了一个问题。我有类似的JSON文件
{"Text":"Here is some text","Make":"Admin","Name":"Hello"}
{"Text":"Here is some text","Make":"John","Name":"Hello"}
{"Text":"Here is some text","Make":"Admin","Name":"Hello"}
我需要从这个文件中读取所有文本。我尝试了,但抛出了异常,这里是我读取的代码
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(new FileReader("Project.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("Text");
System.out.println(name);
}
catch (IOException e) {
e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
Thx帮助
这不是有效的JSON,应该是这样的
[{"Text":"Here is some text","Make":"Admin","Name":"Hello"},
{"Text":"Here is some text","Make":"John","Name":"Hello"},
{"Text":"Here is some text","Make":"Admin","Name":"Hello"}]
obj = parser.parse(new FileReader("Project.json"));
JSONArray jsonArray = (JSONArray) obj;
for (JSONObject jsonObject : jsonArray)
{
String name = (String) jsonObject.get("Text");
System.out.println(name);
}
希望这将解决您的问题