9

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

{
    "categories": {
        "0": {
            "term_id": "247",
            "name": "Content Curation"
        },
        "1": {
            "term_id": "50",
            "name": "Content Marketing"
        },
        "2": {
            "term_id": "2",
            "name": "Curation"
        },
        "3": {
            "term_id": "246",
            "name": "Inbound Marketing"
        },
        "4": {
            "term_id": "47",
            "name": "Marketing"
        },
        "5": {
            "term_id": "4",
            "name": "News Curation"
        },
        "6": {
            "term_id": "36",
            "name": "SEO"
        },
        "8": {
            "term_id": "248",
            "name": "Wordpress Content Curation"
        }
    }
}

My task is to get values of the "term_id" and "name" fields. I've used to get the "categories" object from current JSONObject by using this code:

JSONObject jObject = new JSONObject(responceData);
JSONObject categoryObject = jObject.getJSONObject("categories");
JSONArray  jarray = new JSONArray("["+categoryObject.toString().substring(1,categoryObject.toString().length() - 1) + "]");

for(int i=0;i<jarray.length();i++)
{
     JSONObject jobj = jarray.getJSONObject(i);
     String term_id=jobj.getString("term_id");
     String name=jobj.getString("name");
}

and the categoryObject looks like this:

{
    "0": {
        "term_id": "247",
        "name": "Content Curation"
    },
    "1": {
        "term_id": "50",
        "name": "Content Marketing"
    },
    "2": {
        "term_id": "2",
        "name": "Curation"
    },
    "3": {
        "term_id": "246",
        "name": "Inbound Marketing"
    },
    "4": {
        "term_id": "47",
        "name": "Marketing"
    },
    "5": {
        "term_id": "4",
        "name": "News Curation"
    },
    "6": {
        "term_id": "36",
        "name": "SEO"
    },
    "8": {
        "term_id": "248",
        "name": "Wordpress Content Curation"
    }
}

But after that I don't know how to get the fields. Is there any way to get all JSONObject children from the JSONObject?

If you have a source code or can give me an example please share it with me.

5
  • 1
    the categories should be array. Commented Mar 24, 2014 at 10:24
  • can you post your server code Commented Mar 24, 2014 at 10:31
  • @Dhaval i dont have an access to server side, im parsing it remotely Commented Mar 24, 2014 at 10:32
  • ok so you manually manage the this via code Commented Mar 24, 2014 at 10:33
  • hell checkout the edited source Commented Mar 24, 2014 at 10:53

7 Answers 7

22

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();    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. P.S. I've found a better solution by using your code. Please check my answer for details.
While this may answer OPs question, this way has got great performance loopholes especially with large number of records. You can confirm from here mkyong.com/java/…
please edit String innerKkey = keys.next(); to String innerKkey = innerKeys.next();
5

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

2 Comments

thanks, i will try the code from the answer and tell you if it worked.
Thanks for the help, I've found a solution, please check my answer for details...
4

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

Comments

2

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

Comments

1

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

1 Comment

thanks for the answer, but it is not the best solution for current JSON, because "0" and "1" are dynamic numbers.
1

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

Comments

0

try this ->

String response = data;
JSONObject categories= new JSONObject(data).getJSONObject("categories").getJSONObject('0').getJSONObject('term_id');

print// 247

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.