Check if a particular JSON Object is available or not

44,873

Solution 1

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!

Solution 2

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.

Share:
44,873
curlyreggie
Author by

curlyreggie

Crude Polyglot: Database -> Android -> Python/Django -> Python/JS (AngularJS)

Updated on May 17, 2020

Comments

  • curlyreggie
    curlyreggie almost 4 years

    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
    
  • curlyreggie
    curlyreggie almost 12 years
    But I have problems. How to get the value valueB if the object exists? I tried using the same code. Wasn't able to get it.
  • curlyreggie
    curlyreggie almost 12 years
    Now I reframed it, ` JSONObject getchild2 = jArray.getJSONObject(j).optJSONObject("child2"); if (getchild2 == null) { map.put("rating", "N.A"); } else { map.put("rating", jArray.getJSONObject(j).getString("child2")); }` The fact is, the result should return a value, but is not returning. Always points to "N.A" as the result. What am I doing wrong?
  • CoDe
    CoDe almost 12 years
    i want to check for any json object perticular value exist or not ? like here {"ValidateUserResult":[{"Sys_User_ID":69,"my_id":23}]} i want to check in response "my_id" string is available or not? any one have idea about it?
  • curlyreggie
    curlyreggie almost 12 years
    Extend the same thing as I gave the solution. Should be fine enough.
  • Si8
    Si8 over 10 years
    I have the following JSON file: pagesbyz.com/test.json and I am running into the same issue. Here is my question: stackoverflow.com/questions/19868439/… (If there is a blank entry, the JSON file is not parsed anymore :/) I would really appreciate your help.