5

I have following JSON as response from my server. At first, I thought, it was invalid JSON but after validating it, it seems to be correct:

JOSN: {
    "category": {
        "1": "World",
        "2": "Politics",
        "3": "Economy",
        "4": "Sports",
        "5": "Cricket",
        "6": "General",
        "7": "Business",
        "8": "Services",
        "9": "Law & Order",
        "10": "Entertainment"
    }
}

Validation: JSON Validatiion

If it would have been JSONArray, I would have parsed it with this solution from SO: How to parse a JSON without key in android?

But how do I parse the JSON I have here?

Any help appreciated.

Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126

4 Answers4

16

But how do I parse the JSON I have here?

if keys inside category JSONObject is dynamic then use JSONObject.keys() to get Iterator for getting values as:

JSONObject mainJSONObj=new JSONObject(<json_string>);
// get category JSONObject from mainJSONObj
JSONObject categoryJSONObj=mainJSONObj.getJSONObject("category");

// get all keys from categoryJSONObj

Iterator<String> iterator = categoryJSONObj.keys();
  while (iterator.hasNext()) {
    String key = iterator.next();
    Log.i("TAG","key:"+key +"--Value::"+categoryJSONObj.optString(key);
  }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
11

Try using gson deserialization with an object of such class as a serialization output class:

class MyClass {
    @SerializedName("category")
    private Map<String, String> categories;

    public Map<String, String> getCategories() {
       return categories;
    }

    public void setCategories(Map<String, String> categories) {
        this.categories = categories;
    }
}
rasmeta
  • 465
  • 2
  • 12
1

If I were you and I'm sure that the keys are a sequence of numbers starting with 1, I would do the following:

Map<Integer, String> results = new Hashtable<>();

    try {
        //The response is your JSON as a string
        JSONObject obj = new JSONObject(response);
        JSONObject categories = obj.getJSONObject("categories");

        int counter = 1;
        //Breakable infinite loop, will be broken when you try to get non existing item
        while(true){
            results.put(counter, categories.getString(counter+""));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
//Return the map that contain the results
 return results;

Or using Iterator as the following example :

Map<Integer, String> results = new Hashtable<>();

    try {
        //The response is your JSON as a string
        JSONObject obj = new JSONObject(response);
        JSONObject categories = obj.getJSONObject("categories");

        Iterator<String> iter = categories.keys();
        while (iter.hasNext()) {
            String key = iter.next();
            results.put(key, categories.getString(key+""));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Return the map that contain the results
    return results;

You can also create an ArrayList<String> and add the values to it instead of adding them to a HashTable

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • Thanks for replying. I'm using custom class(bean) for storing list of categories instead of HashMap – GAMA Apr 20 '15 at 10:34
0

Try out this code

JSONObject obj = result.getJSONObject("category");
    Iterator keys = obj.keys();

    while (keys.hasNext()) {
        // loop to get the dynamic key
        String dynamicKey = (String) keys.next();

        String value= obj.getString(dynamicKey);
    }

For more information checkout this link. May be this will help you more.

Community
  • 1
  • 1
cool Boy
  • 326
  • 1
  • 2
  • 9