ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

后端 未结 3 625
一向
一向 2021-01-15 15:44

I am trying to parse a son file, and I don\'t know what I\'m doing wrong (of course, I don\'t really know what I\'m doing right, either).

file.json



        
相关标签:
3条回答
  • 2021-01-15 16:04

    When the JSonParser parses the file, it's returning it as a JSONArray, to solve it try to use this:

        JSONObject obj = (JSONObject)obj;
        JSONObject elem = (JSONObject)obj.get("0");
        String unit = (String) elem.get("elemTwo");
        System.out.println(unit);
    
    0 讨论(0)
  • you are getting JSONArray not JsonObject

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(new FileReader("/path/to/file.json"));
    JSONArray json = (JSONArray) obj;
    

    Than loop this and get jsonobject

    0 讨论(0)
  • 2021-01-15 16:18

    You should cast your obj toJsonArrayinstead of JsonObject, because your json file has [] at the root.

    0 讨论(0)
提交回复
热议问题