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
If it's a JsonArray object, just use getAsJsonArray() to cast it. If not, it's a single element so just add it.
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 .
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; } };
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With