Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if it is JSONObject or JSONArray

Tags:

java

json

I have a json stream which can be something like :

{"intervention":      {        "id":"3",               "subject":"dddd",               "details":"dddd",               "beginDate":"2012-03-08T00:00:00+01:00",               "endDate":"2012-03-18T00:00:00+01:00",               "campus":                        {                           "id":"2",                          "name":"paris"                        }     } } 

or something like

{"intervention":             [{               "id":"1",               "subject":"android",               "details":"test",               "beginDate":"2012-03-26T00:00:00+02:00",               "endDate":"2012-04-09T00:00:00+02:00",               "campus":{                         "id":"1",                         "name":"lille"                        }             },      {      "id":"2",              "subject":"lozlzozlo",              "details":"xxx",              "beginDate":"2012-03-14T00:00:00+01:00",              "endDate":"2012-03-18T00:00:00+01:00",              "campus":{                        "id":"1",                        "name":"lille"                       }             }] }    

In my Java code I do the following:

JSONObject json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream      JSONArray  interventionJsonArray = json.getJSONArray("intervention"); 

In the first case, the above doesn't work because there is only one element in the stream.. How do I check if the stream is an object or an array ?

I tried with json.length() but it didn't work..

Thanks

like image 326
Tang Avatar asked Apr 03 '12 06:04

Tang


People also ask

How do I check if an object is JSONArray or JSON object?

If it's a JsonArray object, just use getAsJsonArray() to cast it. If not, it's a single element so just add it.

How do you check if it is a JSONArray or JSON object in Java?

JSONArray interventions; if(intervention == null) interventions=jsonObject. optJSONArray("intervention"); This will return you an array if it's a valid JSONArray or else it will give null .

How do I check if an object is JSON object?

To check if JavaScript object is JSON, we can use the JSON. parse method. const isJson = (data) => { try { const testIfJson = JSON. parse(data); if (typeof testIfJson === "object") { return true; } else { return false; } } catch { return false; } };


2 Answers

Something like this should do it:

JSONObject json; Object     intervention; JSONArray  interventionJsonArray; JSONObject interventionObject;  json = RestManager.getJSONfromURL(myuri); // retrieve the entire json stream      Object intervention = json.get("intervention"); if (intervention instanceof JSONArray) {     // It's an array     interventionJsonArray = (JSONArray)intervention; } else if (intervention instanceof JSONObject) {     // It's an object     interventionObject = (JSONObject)intervention; } else {     // It's something else, like a string or number } 

This has the advantage of getting the property value from the main JSONObject just once. Since getting the property value involves walking a hash tree or similar, that's useful for performance (for what it's worth).

like image 181
T.J. Crowder Avatar answered Sep 29 '22 17:09

T.J. Crowder


Maybe a check like this?

JSONObject intervention = json.optJSONObject("intervention"); 

This returns a JSONObject or null if the intervention object is not a JSON object. Next, do this:

JSONArray interventions; if(intervention == null)         interventions=jsonObject.optJSONArray("intervention"); 

This will return you an array if it's a valid JSONArray or else it will give null.

like image 29
Nathan Q Avatar answered Sep 29 '22 16:09

Nathan Q