Android 如何在JSON对象中解析多个JSON对象?
我试图解析一个包含多个JSON对象的JSON响应。这是我的密码:Android 如何在JSON对象中解析多个JSON对象?,android,json,Android,Json,我试图解析一个包含多个JSON对象的JSON响应。这是我的密码: { "All": { "name": "All", "display": "All" }, "Apparel": { "name": "Apparel", "display": "Apparel" }, "Appliances": { "name": "Appliances", "display": "Appliances" } } 我尝试了Json中的单对象解析响应,我能够获取它
{
"All": {
"name": "All",
"display": "All"
},
"Apparel": {
"name": "Apparel",
"display": "Apparel"
},
"Appliances": {
"name": "Appliances",
"display": "Appliances"
}
}
我尝试了Json中的单对象解析响应,我能够获取它。但我不知道如何解析具有多个节点的Json对象。我已经试过了,但没能成功。你可以试试下面的代码
JSONObject outer = new JSONObject(response);
Iterator<String> keys =outer.keys();
while(keys.hasNext()){
String key = keys.next();
JSONObject inside = outer.getJSONObject(key);
//Do stuff
}
JSONObject-outer=新的JSONObject(响应);
迭代器键=outer.keys();
while(keys.hasNext()){
String key=keys.next();
JSONObject inside=outer.getJSONObject(键);
//做事
}
其中,
response
是json字符串 如下所示。其中RootData是主json字符串
JSONObject js=new JSONObject(RootData);
JSONObject all =js.getJSONObject(“All”);
String AllName=all.getString(“name”);
String AllDisplay=all.getString(“display”);
JSONObject apparel =js.getJSONObject(“Apparel”);
String apparel_Name=apparel .getString(“name”);
String apparel_Display=apparel .getString(“display”);
JSONObject appliances =js.getJSONObject(“Appliances”);
String appliances_Name=appliances .getString(“name”);
String appliances_Display=appliances .getString(“display”);
您只需获取另一个JSONObect作为请求的键的值, 试试看:
String jsonStr = " {\n\"All\": {\n \"name\": \"All\",\n \"display\": \"All\"\n},\n\"Apparel\": {\n \"name\": \"Apparel\",\n \"display\": \"Apparel\"\n},\n\"Appliances\": {\n \"name\": \"Appliances\",\n \"display\": \"Appliances\"\n}";
JSONObject json;
json = new JSONObject(jsonStr);
JSONObject All = json.getJSONObject("All");
JSON非常简单,您试图解析什么?请显示您的代码。。。到目前为止你都试了些什么?
try {
JSONObject obj = new JSONObject("your result String");
JSONObject obj1 = obj.getJSONObject("All");
String name=obj1.getString("name");
String display=obj1.getString("display");
JSONObject obj2 = obj.getJSONObject("Apparel");
String name1=obj2.getString("name");
String display1=obj2.getString("display");
JSONObject obj3 = obj.getJSONObject("Appliances");
String name2=obj3.getString("name");
String display2=obj3.getString("display");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}