简体   繁体   中英

JSON parsing error with Java org.json JSONObject not found

I have a JSON String that I want to parse with org.json lib:

JSONObject jason_clean = new JSONObject(unzipped);
        JSONArray array = jason_clean.getJSONArray("setlists");
        for (int i=0; i< array.length(); i++) {
            JSONObject tempObject = array.getJSONObject(i);
            JSONObject meta_object = tempObject.getJSONObject("meta");
            String bank_name = meta_object.getString("name");
            System.out.println(bank_name);

            JSONArray presets_array = tempObject.getJSONArray("presets");
            System.out.println("presets_array.length(): "+presets_array.length());
            
            for (int j=0; j< presets_array.length(); j++) {
                JSONObject temp_preset_Object = presets_array.getJSONObject(j);
                //if (temp_preset_Object.has("meta")) {
                JSONObject meta_preset_object = temp_preset_Object.getJSONObject("meta");
                String preset_name = meta_preset_object.getString("name");
                System.out.println("BANK: "+i+" - "+bank_name+" || "+j+" - "+preset_name);
                //}
                
            }
        
        }

I get many results like I have been expected but it stops with the following error stack:

Exception in thread "main" org.json.JSONException: JSONObject["meta"] not found.
    at org.json.JSONObject.get(JSONObject.java:573)
    at org.json.JSONObject.getJSONObject(JSONObject.java:766)
    at ZLibCompression.main(ZLibCompression.java:68)

Then I tried to test, if there is a "meta"-object (commented out in the code above). Now the parser runs through the end of the String but there are many entries missing. (Not only at that point, where it had stopped before.) I have validated the JSON string with a software validator (and my python script can handle the whole String in the expected way.) I'm new to JSON, so I don't know, if it could be a parser Problem or if there is an error in my code to handle this specific String. I've linked the JSON-String-file here. (8.6 MB)

I've added the formatted output of successfully parsed elements

Here are some snippets of the JSON string which are parsed or not parsed (JSONObject["meta"] not found):

with exception (line 213784 in JSON string):

{
     "device" : 2162692,
     "device_version" : 50397184,
     "meta" : {
      "build_sha" : "39f7f9a",
      "name" : ""
     },

without exception (line 304922 in JSON string):

{
     "device" : 2162692,
     "device_version" : 50397184,
     "meta" : {
      "build_sha" : "v2.81-16-gbdc0fd8",
      "name" : ""
     },

with exception (line 176330 in JSON string):

{
     "device" : 2162692,
     "device_version" : 50397184,
     "meta" : {
      "build_sha" : "561c612",
      "name" : "SLAP Punch"
     },

It is failing here --> JSONObject meta_preset_object = temp_preset_Object.getJSONObject("meta");

presets under --- USER 4 does not have meta.

    "meta": {
        "name": "USER 4"
    },
    "presets": []

Your presets should have data similar to all the others:

"device": , "device_version": , "meta": { "build_sha": "", "name": "" },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM