我有一个需要解析的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'有时为null,有时是string。那么,我该怎么做呢?另外,只要CC的“值”为空,如何继续解析下一个对象?
编辑:所以,我放了一个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";
}
}
答案 0 :(得分:2)
你的json无效。请验证您的json,然后尝试使用您的代码。
使用http://jsonlint.com/验证您的输入json。
答案 1 :(得分:0)
正如@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");
当迭代我们数组中的第3个元素时,该对象没有键Value
,因此你放在get
上的getJSONObject(i).get("Value")
调用会给你一个{ {1}}因为对象中没有这样的键。
请参阅文档here
答案 2 :(得分:0)
首先你的json无效 它应该是那样的
{
"Event": {
"Details": [
{
"Key": "AA",
"Value": "a"
},
{
"Key": "BB",
"Value": "B"
},
{
"Key": "CC"
},
{
"Key": "MIN",
"Value": -1
}
]
}
}
现在你的解析代码需要检查&#34; Value&#34;存在的关键是存在然后只得到&#34;值&#34;键
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());
}
}