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