Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a particular JSON Object is available or not

I have JSON File as below which has to be dumped into an 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.

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

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/

It doesn't work as error is below:

E/AndroidRuntime(664): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
like image 663
curlyreggie Avatar asked May 15 '12 04:05

curlyreggie


People also ask

How do you check if a JSON object is available or not?

Use below code to find key is exist or not in JsonObject . has("key") method is used to find keys in JsonObject . If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject . Note that you can check only root keys with has(). Get values with get().

How check JSON key exists or not in PHP?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.


2 Answers

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.

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

Thanks everyone for the help!

like image 102
curlyreggie Avatar answered Oct 12 '22 22:10

curlyreggie


getJSONObject("child2");

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

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.

like image 34
Abdullah Jibaly Avatar answered Oct 12 '22 23:10

Abdullah Jibaly