解析下面的JSON文件時出現錯誤。如果有人請幫助我,非常感謝!使用Java解析JSON文件時出錯
{"Name":"Abc", "Author":"fgd", "Company List":{"Company":"C1","Companyone":"Compa2"}}
這裏是我的代碼: -
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("/Users/abcdefgh/Documents/File1.txt"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("Name");
String author = (String) jsonObject.get("Author");
System.out.println("Name: " + name);
System.out.println("Author: " + author);
// JSONArray companyList= new JSONArray();
//companyList.add(obj);
JSONArray companyList = (JSONArray) jsonObject.get("Company List");
System.out.println("Company List:");
/* for (int i=0;i<2;i++){
System.out.println(companyList.get(i));
}
*/Iterator<String> iterator = companyList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
輸出: -
名稱:農行
作者:FGD
java.lang.ClassCastException:組織.json.simple.JSONObject不能爲 ca第一至在 com.selenium.test.JSONRead.main(JSONRead.java:29)
感謝您的回覆!使用getJSONObject,我能夠成功解析它。 –