Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

java - Android JSON parsing of multiple JSONObjects inside JSONObject

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...