Java Android JSON 解析 JSONObject 中的多个 JSONObjects
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22606572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android JSON parsing of multiple JSONObjects inside JSONObject
提问by Arshak92
I have a JSON string coming from the server and it looks like this:
我有一个来自服务器的 JSON 字符串,它看起来像这样:
{
"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:
我的任务是获取“term_id”和“name”字段的值。我曾经使用以下代码从当前 JSONObject 获取“类别”对象:
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:
和categoryObject
看起来像这样:
{
"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?
但在那之后我不知道如何获得这些字段。有没有办法从 JSONObject 中获取所有 JSONObject 子项?
If you have a source code or can give me an example please share it with me.
如果您有源代码或可以给我一个示例,请与我分享。
采纳答案by Ahmad Dwaik 'Warlock'
here you can retrieve all your json data, ask for a specific key and innerKey to get what you want, cheers
在这里你可以检索你所有的 json 数据,要求一个特定的 key 和 innerKey 来得到你想要的,干杯
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(); }
回答by TMH
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
检查这个答案,这样你就不需要知道键,并且可以循环并访问内部对象,但类别作为 JSONArray 而不是对象会更好,这样你就可以正常循环
for(int i = 0; i < array.length(); i++) {
JSONObject obj = myArray.get(i);
....
}
回答by kaushik parmar
Use the keys() iterator to iterate over all the properties, and call get() for each.
使用 keys() 迭代器迭代所有属性,并为每个属性调用 get() 。
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
回答by cgr
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
但我同意 wqrahd,类别应该是一个数组
回答by Arshak92
Big thanks to Ahmad Dwaik and Tom Hart fot the answers. This is the code for solution.
非常感谢 Ahmad Dwaik 和 Tom Hart 的回答。这是解决方案的代码。
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();
}
回答by Satyam Singh
While Ahmad Dwaik 'Warlock' has provided a very good answer but there is a mistake in his code current code is --
虽然 Ahmad Dwaik 'Warlock' 提供了一个很好的答案,但他的代码中有一个错误,当前代码是——
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(); }