简体   繁体   English

JSON 解析错误 Java org.json JSONObject not found

[英]JSON parsing error with Java org.json JSONObject not found

I have a JSON String that I want to parse with org.json lib:我有一个 JSON 字符串,我想用 org.json 库解析它:

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. (不仅在那个时候,它之前已经停止了。)我已经使用软件验证器验证了 JSON 字符串(并且我的 python 脚本可以以预期的方式处理整个字符串。)我是 Z0ECD11C1D7A287401D148A2 的新手,所以我不要'不知道,如果它可能是解析器问题,或者我的代码中是否有错误来处理这个特定的字符串。 I've linked the JSON-String-file here. 我在这里链接了 JSON-String-file。 (8.6 MB) (8.6 MB)

I've added the formatted output of successfully parsed elements我添加了成功解析元素的格式化 output

Here are some snippets of the JSON string which are parsed or not parsed (JSONObject["meta"] not found):以下是 JSON 字符串的一些片段,它们已解析或未解析(未找到 JSONObject["meta"]):

with exception (line 213784 in JSON string):有例外(JSON 字符串中的第 213784 行):

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

without exception (line 304922 in JSON string):无一例外(JSON 字符串中的第 304922 行):

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

with exception (line 176330 in JSON string):有例外(JSON 字符串中的第 176330 行):

{
     "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");这里失败了——> JSONObject meta_preset_object = temp_preset_Object.getJSONObject("meta");

presets under --- USER 4 does not have meta. --- USER 4 下的预设没有元。

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

Your presets should have data similar to all the others:您的预设应该具有与所有其他类似的数据:

"device": , "device_version": , "meta": { "build_sha": "", "name": "" }, “设备”:,“设备版本”:,“元”:{“build_sha”:“”,“名称”:“”},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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