Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I find out a JSON Object return JSON Array or string in android

Tags:

java

json

android

I have a json like the following. how do I find out a JSON Object return JSON Array or string in android.

{
    "green_spots": [
    ......
    ],
    "yellow_spots": "No yellow spot available",
    "red_spots": "No red spot available"
}

The JSON objects retrurn Array when values is present else return a String like "No green/red/yellow spot available". I done the with following way. but is there any other way to do? because alert string is changed the If will not work.

JSONObject obj = new JSONObject(response);
String green = obj.getString("green_spots");

// Green spots
if ("No green spot available".equalsIgnoreCase(green)) {
    Log.v("search by hour", "No green spot available");
} else {
    JSONArray greenArray = obj.getJSONArray("green_spots");
            ....
      }
like image 803
M.A.Murali Avatar asked Jan 31 '26 10:01

M.A.Murali


1 Answers

    Object object = jsonObject.get("key");
    if (object instanceof JSONObject) {
    // It is json object
    } else if (object instanceof JSONArray) {
    // It is Json Array
    } else {
    // It is a String
    }
like image 68
Jagadesh Seeram Avatar answered Feb 03 '26 03:02

Jagadesh Seeram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!