Android JSON parsing of multiple JSONObjects inside JSONObject

后端 未结 6 1575
面向向阳花
面向向阳花 2020-11-29 10:28

I have a JSON string coming from the server and it looks like this:

{
    \"categories\": {
        \"0\": {
            \"term_id\": \"247\",
            \"         


        
相关标签:
6条回答
  • 2020-11-29 11:03

    Big thanks to Ahmad Dwaik and Tom Hart fot the answers. This is the code for solution.

    try
            {   
    
                JSONObject jObject= new JSONObject(responseData).getJSONObject("categories");
                Iterator<String> keys = jObject.keys();
                while( keys.hasNext() )
                {
                    String key = keys.next();
                    Log.v("**********", "**********");
                    Log.v("category key", key);
                    JSONObject innerJObject = jObject.getJSONObject(key);
    
                    String name = innerJObject.getString("name");
                    String term_id = innerJObject.getString("term_id");
    
                    Log.v("name = "+name, "term_id = "+term_id);
    
                }
            }
            catch (JSONException e){
               e.printStackTrace();    
            }
    
    0 讨论(0)
  • 2020-11-29 11:04

    Use the keys() iterator to iterate over all the properties, and call get() for each.

    Iterator<String> iter = json.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        try {
            Object value = json.get(key);
        } catch (JSONException e) {
            // Something went wrong!
        }
    }
    

    See this : https://stackoverflow.com/a/13573965/2480911

    0 讨论(0)
  • 2020-11-29 11:15

    Check this answer , that way you don't need to know the keys, and can just loop through and access the inner object, but categories would be better as a JSONArray rather than an Object, that way you could just normally loop through

    for(int i = 0; i < array.length(); i++) {
        JSONObject obj = myArray.get(i);
        ....
    }
    
    0 讨论(0)
  • 2020-11-29 11:15

    try this

    JSONObject jObject = new JSONObject(responceData);
    JSONObject categoryObject = jObject.getJSONObject("categories");
    JSONObject obj0 = categoryObject.getJSONObject("0");
    String termId0 obj0.getString("term_id");
    String name0 obj0.getString("name");
    JSONObject obj1 = categoryObject.getJSONObject("1");
    String termId1 obj1.getString("term_id");
    String name1 obj1.getString("name");
    

    but i agree with wqrahd, categories should be an array

    0 讨论(0)
  • 2020-11-29 11:15

    While Ahmad Dwaik 'Warlock' has provided a very good answer but there is a mistake in his code current code is --

    try
    {   
        String jsonString="";//your json string here
        JSONObject jObject= new JSONObject(jsonString).getJSONObject("categories");
        Iterator<String> keys = jObject.keys();
        while( keys.hasNext() )
        {
            String key = keys.next();
            Log.v("**********", "**********");
            Log.v("category key", key);
            JSONObject innerJObject = jObject.getJSONObject(key);
            Iterator<String> innerKeys = innerJObject.keys();
            while( innerKeys.hasNext() )
            {
                String innerKkey = innerKeys.next();  //Here was the error
                String value = innerJObject.getString(innerKkey);
                Log.v("key = "+key, "value = "+value);
            }
        }
    }
    catch (JSONException e)
    {   e.printStackTrace();    }
    
    0 讨论(0)
  • 2020-11-29 11:20

    here you can retrieve all your json data, ask for a specific key and innerKey to get what you want, cheers

        try
        {   
            String jsonString="";//your json string here
            JSONObject jObject= new JSONObject(jsonString).getJSONObject("categories");
            Iterator<String> keys = jObject.keys();
            while( keys.hasNext() )
            {
                String key = keys.next();
                Log.v("**********", "**********");
                Log.v("category key", key);
                JSONObject innerJObject = jObject.getJSONObject(key);
                Iterator<String> innerKeys = innerJObject.keys();
                while( innerKeys.hasNext() )
                {
                    String innerKkey = keys.next();
                    String value = innerJObject.getString(innerKkey);
                    Log.v("key = "+key, "value = "+value);
                }
            }
        }
        catch (JSONException e)
        {   e.printStackTrace();    }
    
    0 讨论(0)
提交回复
热议问题