【问题标题】:JSONArray not getting properly parsedJSONArray 没有得到正确解析
【发布时间】:2015-07-01 05:31:41
【问题描述】:

我有一个需要解析的 JSONArray。但是,问题是其中一个对象只有一个字段,其余对象有两个字段。

{"Event":{
  "Details":[
  {
      "Key" : "AA",
      "Value" : "a"
  },
  {
      "Key" : "BB",
      "Value" :"B"
  },
  {
      "Key" :"CC"
  },
  {
      "Key" :"MIN",
      "Value" : -1
  }
]
}}

所以,现在当我使用这段代码解析它时

private void parse(String jsonStr) throws JSONException{
    String Value, Key;
    JSONObject obj = new JSONObject(jsonStr);
    JSONArray array = obj.getJSONObject("Event").getJSONArray("Details");
    for(int i = 0; i < array.length(); i++) {
        Key = array.getJSONObject(i).getString("Key");
        if(Key.equals("CC")){   // make case for null here
            Value = "Something was supposed to be here";
        }

        Object type = array.getJSONObject(i).get("Value");
        //System.out.println("Type: \n"+type.getClass().getSimpleName());
        if(type instanceof String) {
            String ValueStr=array.getJSONObject(i).getString("Value");
            System.out.println(ValueStr);
        } else if(type instanceof Number) {
            Integer ValueInt = array.getJSONObject(i).getInt("Value");
            System.out.println(ValueInt);
        }
    }
}

读取“CC”后停止解析。它甚至不读取下一个值。 'Key' CC 的'Value' 有时为空,有时为字符串。那么,我该如何捕捉呢?另外,CC的Value为null时,如何继续解析下一个对象?

编辑: 所以,我放了一个 try-catch 块,但 for 循环甚至不再迭代了。它只循环一次。

for(int i =0; i<array.length() ; i++)
    {
        Key = array.getJSONObject(i).getString("Key");

        try
        {           
                 Object type = array.getJSONObject(i).get("Value");
        //System.out.println("Type: \n"+type.getClass().getSimpleName());

            if(type instanceof String)
            {           
                String ValueStr = array.getJSONObject(i).getString("Value");
                System.out.println(ValueStr);               
            }

            else if(type instanceof Number)
            {       
                Integer ValueInt = array.getJSONObject(i).getInt("Value");
                System.out.println(ValueInt);
            }
        }
        catch(JSONException e){
            Value="something was supposed to be here";
        }

    }

【问题讨论】:

  • 显然我也无法正确格式化我的代码。 ://
  • 请先验证您的 json,` "Key" : "AA", "Value" : "a"` 应该是逗号分隔值,然后尝试您的代码
  • 对不起,这是我打字的错误。 JSONArray 很好,它是在另一种方法中生成的。再次,对此感到抱歉。我现在编辑了它
  • @user270386:检查下面给出的答案。

标签: java arrays json parsing


【解决方案1】:

您的 json 无效。请验证您的 json,然后尝试您的代码。

使用 http://jsonlint.com/ 验证您的输入 json。

【讨论】:

  • 虽然json输入无效,但实际上并没有回答问题
  • 他仍然会遇到一个例外。
【解决方案2】:

正如@Durga 在她的评论中指出的那样,您缺少内部 json 对象中的逗号。

你的 JSON 应该是

{"Event":{
  "Details":[
  {
      "Key" : "AA", // see the commas here?
      "Value" : "a"
  },
  {
      "Key" : "BB", // see the commas here?
      "Value" :"B"
  },
  {
      "Key" :"CC"
  },
  {
      "Key" :"MIN", // see the commas here?
      "Value" : -1
  }
]
}}

现在对于第二部分,您的 java 代码可能会抛出 JSONException

因为这里

Object type = array.getJSONObject(i).get("Value");

当迭代我们数组中的第三个元素时,该对​​象没有键 Value,因此您在 getJSONObject(i).get("Value") 上的 get 调用将抛出一个 JSONException,因为没有对象中的此类键。

参见文档here

【讨论】:

    【解决方案3】:

    首先你的 json 是无效的 应该是这样的

    {
    "Event": {
        "Details": [
            {
                "Key": "AA",
                "Value": "a"
            },
            {
                "Key": "BB",
                "Value": "B"
            },
            {
                "Key": "CC"
            },
            {
                "Key": "MIN",
                "Value": -1
            }
        ]
    }
    

    }

    现在您的解析代码需要检查“Value”键是否存在,然后只获取“Value”键的值

    private void parse(String jsonStr) throws JSONException
    {
    
        String Value, Key;
        JSONObject obj = new JSONObject(jsonStr);
        JSONArray array = obj.getJSONObject("Event").getJSONArray("Details");
        for(int i =0; i<array.length() ; i++)
        {
            Key = array.getJSONObject(i).getString("Key");
    
            //if(Key.equals("CC"))    // comment this
            if(!array.getJSONObject(i).has("Value")) //if "Value" key is not exist
            {
                Value = "Something was supposed to be here";
            }
            else //if "Value" key exist
            {
               //do whatever u like
               Object type = array.getJSONObject(i).get("Value");
               if(type instanceof String)
               {           
                String ValueStr=array.getJSONObject(i).getString("Value");
                System.out.println(ValueStr);               
                }
    
                else if(type instanceof Number)
                {       
                Integer ValueInt = array.getJSONObject(i).getInt("Value");
                System.out.println(ValueInt);
                }
            }
            //Object type = array.getJSONObject(i).get("Value");//put this line inside else part
            //System.out.println("Type: \n"+type.getClass().getSimpleName());
        }
     }
    

    【讨论】:

    • -1。你还是有问题。 Object type = array.getJSONObject(i).get("Value"); 当然仍然会抛出异常。应该对其进行重组。你可能应该把它放在 else 块中。此外,您对 if else 块的设计是完全错误的。当您可以简单地将 else 中的代码交换到 if 块(反之亦然)并删除否定时,为什么要否定条件?
    • 我已经删除了反对票,因为现在可以防止异常。但 if 块可以改进。删除否定运算符并交换 if 和 else 块中的代码。 if(true){ do A} else { do B} is always gud than, if(!true) {do B} else { do A}
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2016-05-30
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多