Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

json

getjson

I am getting JSON string from website. I have data which looks like this (JSON Array)

 myconf= {URL:[blah,blah]}

but some times this data can be (JSON object)

 myconf= {URL:{try}}

also it can be empty

 myconf= {}    

I want to do different operations when its object and different when its an array. Till now in my code I was trying to consider only arrays so I am getting following exception. But I am not able to check for objects or arrays.

I am getting following exception

    org.json.JSONException: JSONObject["URL"] is not a JSONArray.

Can anyone suggest how it can be fixed. Here I know that objects and arrays are the instances of the JSON object. But I couldn't find a function with which I can check whether the given instance is a array or object.

I have tried using this if condition but with no success

if ( myconf.length() == 0 ||myconf.has("URL")!=true||myconf.getJSONArray("URL").length()==0)
like image 474
Judy Avatar asked Mar 22 '12 06:03

Judy


People also ask

How do I check if a JSON value is an array?

Check if Variable is Array ? Sometimes, you need to check a parsed JSON object or a variable to see if it's an array before iteration or before any other manipulation. You can use the Array. isArray method to check if a variable is an array.

How do I check if an object is in JSON format?

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

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
}
like image 133
cHao Avatar answered Sep 22 '22 06:09

cHao


Quite a few ways.

This one is less recommended if you are concerned with system resource issues / misuse of using Java exceptions to determine an array or object.

try{
 // codes to get JSON object
} catch (JSONException e){
 // codes to get JSON array
}

Or

This is recommended.

if (json instanceof Array) {
    // get JSON array
} else {
    // get JSON object
}
like image 20
Oh Chin Boon Avatar answered Sep 22 '22 06:09

Oh Chin Boon