2

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");
            ....
      }

5 Answers 5

9
    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
    }
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, every time i am confuse about json and this is best example to find object array and string of JSON data.
1

You can use instanceof

instead of getString do just obj.get which will return an Object, check if the object is instanceof String or JSONArray

EDIT:

here is a bit of sample code to go with this:

Object itineraries = planObject.get("itineraries");
if (itineraries instanceof JSONObject) {
    JSONObject itinerary = (JSONObject) itineraries;
    // right now, itinerary is your single item
}
else {
    JSONArray array = (JSONArray) itineraries;
    // do whatever you want with the array of itineraries
}

Comments

0
JSONObject obj = new JSONObject(response);
JSONArray greenArray = obj.getJSONArray("green_spots");
if(greenArray!=null){
     do your work with greenArray here
}else{
    Log.v("search by hour", "No green spot available");
}

Comments

0

Simple just print the object like Log.e("TAG","See>>"JsonObject.toString); if response is in {} block then it is object if it is in [] its array

Comments

0

Warning: This information may be superfluous, but it might prove to be an alternative approach to this problem.

You can use Jackson Object Mapper to convert a JSON file to a HashMap.

public static HashMap<String, Object> jsonToHashMap(
            String jsonString) {

        Map<String, Object> map = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();

        try {

            // convert JSON string to Map
            map = mapper.readValue(jsonString,
                    new TypeReference<HashMap<String, Object>>() {
                    });


        } catch (Exception e) {
            e.printStackTrace();
        }
        return (HashMap<String, Object>) map;
    }

This automatically creates a HashMap of appropriate objects. You can then use instanceof or figure out another way to use those objects as appropriate/required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.