如何保存在变量中?
但是有了这个 json 代码:
{
"restarutant": [
{
"name": "Hotel Raja",
"photo": "http:\/\/i.imgur.com\/Mzt4u.jpg",
"address": "93, 2ndc ross, GDP etx.",
"area": "Vylaikaval",
"city": "Bangalore",
"rating": "4",
"cuisines": {
"first": "Chinese",
"second": "Korean"
}
},
{
"name": "Hotel Raja2",
"photo": "http:\/\/i.imgur2.com\/Mzt4u.jpg",
"address": "93, 2ndc ross, GDP etx. number2",
"area": "Vylaikaval2",
"city": "Bangalore2",
"rating": "4",
"cuisines": {
"first": "Chinese2",
"second": "Korean2"
}
}
]
}
代码:
JSONObject json = new JSONObject(thepreviousjson);
JSONArray jArray = json.getJSONArray("restaurant");
String name[] = new String[jArray.length()];
String photo[] = new String[jArray.length()];
String address[] = new String[jArray.length()];
String area[] = new String[jArray.length()];
String city[] = new String[jArray.length()];
String rating[] = new String[jArray.length()];
String cuisines[] = new String[jArray.length()];
String first[] = new String[jArray.length()];
String second[] = new String[jArray.length()];
for(int i=0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
name[i] = json_data.getString("name");
photo[i] = json_data.getString("photo");
address[i] = json_data.getString("address");
area[i] = json_data.getString("area");
city[i] = json_data.getString("city");
rating[i] = json_data.getString("rating");
}
关键是要存储:
name[0] = "Hotel Raja"...
name[1] = "Hotel Raja2"
first[0] = Chinese, second[0] = Korean, first[1] = Chinese2, second[1] = Korean2
我尝试了几种组合,但没有任何反应,我需要在我的代码中修改什么?谢谢