Determine Whether JSON Is a JSONobject or JSONarray

Determine whether JSON is a JSONObject or JSONArray

I found better way to determine:

String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
//you have an object
else if (json instanceof JSONArray)
//you have an array

tokenizer is able to return more types: http://developer.android.com/reference/org/json/JSONTokener.html#nextValue()

Test if it is JSONObject or JSONArray

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).

Test if it is JSONObject or JSONArray

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).

JSON Java check element is a JSONArray or JSONObject

Yes, this is because the getJSONObject("category") will try to convert that String to a JSONObject what which will throw a JSONException. You should do the following:

Check if that object is a JSONObject by using:

   JSONObject category=jsonObject.optJSONObject("Category");

which will return a JSONObject or null if the category object is not a json object.
Then you do the following:

   JSONArray categories;
if(category == null)
categories=jsonObject.optJSONArray("Category");

which will return your JSONArray or null if it is not a valid JSONArray .

How to check if it is a JsonArray or JsonObject in Javascript

You can use Array.isArray to check if it is array:

if (Array.isArray(json['amazon'])) {
// It is array
}

and you can check if type of object is object to determine if it is an object. Please refer, for example, this answer for the reason why check for null is separate:

if (json['flipkart'] !== null && typeof (json['flipkart']) === 'object') {
// It is object
}

How to tell if return is JSONObject or JSONArray with JSON-simple (Java)?

Simple Java:

Object obj = new JSONParser().parse(result); 
if (obj instanceof JSONObject) {
JSONObject jo = (JSONObject) obj;
} else {
JSONArray ja = (JSONArray) obj;
}

You could also test if the (purported) JSON starts with a [ or a { if you wanted to avoid the overhead of parsing the wrong kind of JSON. But be careful with leading whitespace.

How do I detect if an embedded JSON element is a JSONObject or JSONArray in Java

You can use optJSONObject() & optJSONArray() methods to check if the underlying is an Object or Array. Below is an example for that.

JSONObject json = new JSONObject("{\"key1\": \"val1\",\n" + 
" \"key2\": {\"level2\": {\"level3\": \"val3\"}}\n" +
"}");

JSONObject json1 = new JSONObject("{\"key1\": \"val1\",\n" +
" \"key2\": {\"level2\": [{\"level3\": \"val3\"}]}\n" +
"}");

if(json.getJSONObject("key2").optJSONObject("level2") != null) {
System.out.println("json : it is a jsonobject");
}
else if(json.getJSONObject("key2").optJSONArray("level2") != null) {
System.out.println("json : it is a jsonarray");
}


if(json1.getJSONObject("key2").optJSONObject("level2") != null) {
System.out.println("json1 : it is a jsonobject");
}
else if(json1.getJSONObject("key2").optJSONArray("level2") != null) {
System.out.println("json1 : it is a jsonarray");
}

How to check whether the given object is object or Array in JSON string

JSON objects and arrays are instances of JSONObject and JSONArray, respectively. Add to that the fact that JSONObject has a get method that will return you an object you can check the type of yourself without worrying about ClassCastExceptions, and there ya go.

if (!json.isNull("URL"))
{
// Note, not `getJSONArray` or any of that.
// This will give us whatever's at "URL", regardless of its type.
Object item = json.get("URL");

// `instanceof` tells us whether the object can be cast to a specific type
if (item instanceof JSONArray)
{
// it's an array
JSONArray urlArray = (JSONArray) item;
// do all kinds of JSONArray'ish things with urlArray
}
else
{
// if you know it's either an array or an object, then it's an object
JSONObject urlObject = (JSONObject) item;
// do objecty stuff with urlObject
}
}
else
{
// URL is null/undefined
// oh noes
}


Related Topics



Leave a reply



Submit