0
我试图从一个文件中的这个JSON数据读取如何在java中读取json文件?
[{"name":"Luke","score":50},{"name":"Ryan","score":70}]
代码:
JsonParser parser = new JsonParser();
try{
Object obj = parser.parse(new FileReader("C:/Users/user/Documents/HighscoresJSON.json"));
JSONObject jsonObject = (JSONObject) obj;
String usersName = (String) jsonObject.get("name");
System.out.println("Name of user: " + usersName);
int usersScore = (int) jsonObject.get("score");
System.out.println("Score: " + usersScore);
这给我的错误:
com.google.gson.JsonArray cannot be cast to org.json.JSONObject
感谢您的答复,非常有帮助的,唯一的问题是我得到了,说只能遍历一个循环jsonArray下一个错误数组或java.lang.Iterable的实例 – LC12382
完美,非常感谢。 – LC12382