Android JSON parsing of multiple JSONObjects inside JSONObject

后端 未结 6 1587
面向向阳花
面向向阳花 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: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 keys = jObject.keys();
            while( keys.hasNext() )
            {
                String key = keys.next();
                Log.v("**********", "**********");
                Log.v("category key", key);
                JSONObject innerJObject = jObject.getJSONObject(key);
                Iterator 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();    }
    

提交回复
热议问题