简体   繁体   中英

how do I find out a JSON Object return JSON Array or string in 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");
            ....
      }
    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
    }

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM