Parsing JSONObject having dynamic key

后端 未结 4 1562
感情败类
感情败类 2021-01-03 07:34

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: {
    \"cat         


        
相关标签:
4条回答
  • 2021-01-03 08:03

    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.

    0 讨论(0)
  • 2021-01-03 08:14

    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

    0 讨论(0)
  • 2021-01-03 08:21

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 08:26

    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);
      }
    
    0 讨论(0)
提交回复
热议问题