检查特定的JSON对象是否可用

时间:2021-11-20 16:54:53

I have JSON File as below which has to be dumped into an ArrayList:

我有下面的JSON文件,它必须被转储到ArrayList中:

{
 "main1" : [
  {
     "child1" : valueA,
     "child2" : valueB,
     "child3" : valueC,
  },
  {
     "child1" : value1,
     "child3" : value3,
  },
 ],
 "main2" : "valueMain2"
}

The element child2 has to be checked if it exists or not and then the value is taken. You can see that it doesn't appear in the second array.

必须检查元素child2是否存在,然后获取值。你可以看到它没有出现在第二个数组中。

I am using Native JSON (org.JSON) Java Code is below:

我使用的是原生JSON (org.JSON) Java代码如下:

ArrayList<HashMap<String,String>> myList = new ArrayList<HashMap<String,String>>();
JSONObject json = <my_json_object>;

JSONObject getchild2;

JSONArray jArray = <my_json_object>.getJSONArray("main1");
for(int j = 0; j < jArray.length(), j++){
  HashMap<String,String> map = new HashMap<String,String>();

  map.put("First Value", jArray.getJSONObject(j).getString("child1"));
  map.put("Third Value", jArray.getJSONObject(j).getString("child3"));

  getchild2 = jArray.getJSONObject(j).getJSONObject("child2");
  if(getchild2 == null){
    map.put("Second Value", "Some Dummy Value");
  } else {
    map.put("Second Value", jArray.getJSONObject(j).getString("child2"));
  }
 myList.add(map);
}

How can I achieve this?

我怎样才能做到这一点呢?

<my_json_object> uses native URL Parsing as I get the HTTP Response of a URL request found here: http://mobisys.in/blog/2012/01/parsing-json-from-url-in-android/

使用本地URL解析,因为我在这里找到了一个URL请求的HTTP响应:http://mobisys.in/blog2//2012/01/par- json-from-url-in-android/

It doesn't work as error is below:

它不工作,因为错误如下:

E/AndroidRuntime(664): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

3 个解决方案

#1


37  

Anyway, I got the solution with some Googling: I re-framed it as,

不管怎样,我通过谷歌找到了解决方案:我把它重新定义为,

 if (!jArray.getJSONObject(j).has("child2")) {
  map.put("Second Value", "N.A");
 } else {
  map.put("Second Value", jArray.getJSONObject(j).getString("child2"));
 }

Creating the JSONObject getchild2 = jArray.getJSONObject(j).getJSONObject("child2"); was rather, unnecessary.

创建JSONObject getchild2 = jArray.getJSONObject(j).getJSONObject("child2");相反,是不必要的。

And this works, rather perfectly! Refer this for more details: Checking if exists subObject in JSON

这个方法非常有效!有关更多细节,请参阅本文:检查JSON中是否存在子对象

Thanks everyone for the help!

谢谢大家的帮助!

#2


11  

getJSONObject("child2");

Will throw an exception if child2 does not exist. Try this instead:

如果child2不存在,将抛出异常。试试这个:

getchild2 = jArray.getJSONObject(j).optJSONObject("child2");

That way you don't have to catch an exception if child2 doesn't exist and can instead check for null.

这样,如果child2不存在,就不需要捕获异常,而可以检查null。

#3


-2  

Try this:

试试这个:

{"main1" : [{
             "id":"1"
               "child1" : "valueA",
               "child2" : "valueB",
               "child3" : "valueC",
            }, {
                "id":"2"
                  "child1" : "value1",
                 "child3"  : "value3",
           }]
}

#1


37  

Anyway, I got the solution with some Googling: I re-framed it as,

不管怎样,我通过谷歌找到了解决方案:我把它重新定义为,

 if (!jArray.getJSONObject(j).has("child2")) {
  map.put("Second Value", "N.A");
 } else {
  map.put("Second Value", jArray.getJSONObject(j).getString("child2"));
 }

Creating the JSONObject getchild2 = jArray.getJSONObject(j).getJSONObject("child2"); was rather, unnecessary.

创建JSONObject getchild2 = jArray.getJSONObject(j).getJSONObject("child2");相反,是不必要的。

And this works, rather perfectly! Refer this for more details: Checking if exists subObject in JSON

这个方法非常有效!有关更多细节,请参阅本文:检查JSON中是否存在子对象

Thanks everyone for the help!

谢谢大家的帮助!

#2


11  

getJSONObject("child2");

Will throw an exception if child2 does not exist. Try this instead:

如果child2不存在,将抛出异常。试试这个:

getchild2 = jArray.getJSONObject(j).optJSONObject("child2");

That way you don't have to catch an exception if child2 doesn't exist and can instead check for null.

这样,如果child2不存在,就不需要捕获异常,而可以检查null。

#3


-2  

Try this:

试试这个:

{"main1" : [{
             "id":"1"
               "child1" : "valueA",
               "child2" : "valueB",
               "child3" : "valueC",
            }, {
                "id":"2"
                  "child1" : "value1",
                 "child3"  : "value3",
           }]
}