如何使用不在JSON中但仍然是我在android中使用PHP接收的数组元素的列名称? 这是我的PHP文件:
{
"items": [
{
"v_id": "1",
"v_name": "Hotel Girija",
"v_address": "Vijayanagar Colony, Municipal Colony, Pune, Maharashtra 411030",
"v_avg_rating": "3",
"v_image": "http://i1.walesonline.co.uk/whats-on/food-drink-news/article7978981.ece/ALTERNATES/s615/MAin.jpg",
"v_contact1": "8456217412"
},
{
"v_id": "4",
"v_name": "Pataleshwar hotel",
"v_address": "Some addresss again",
"v_avg_rating": "5",
"v_image": "https://www.whatsuplife.in/gurgaon/blog/wp-content/uploads/2015/09/Chaat-Chowk.jpg",
"v_contact1": "5452232554"
}
],
"sub_image": "http://www.asiansinamerica.org/wp- content/uploads/2016/08/Asian-and-Chinese-Food.jpg",
"success": 1
}
我必须使用以下内容获取关键项的所有值:
JSONObject root = new JSONObject(items);
JSONArray arr = root.getJSONArray("items");
for (int i = 0; i < arr.length(); i++)
{
JSONObject obj = (JSONObject) arr.get(i);
path = "" + obj.optString("v_image");
// path = "http://" + path.replaceAll("\\s+", "%20");
al_subcatdetl_id.add("" + obj.optString("v_id"));
al_subcatdetl_name.add("" + obj.optString("v_name"));
al_subcatdetl_addr.add("" + obj.optString("v_address"));
al_subcatdetl_img.add("" + path);
al_subcatdetl_rating.add("" + obj.optString("v_avg_rating"));
///al_subject_img.add(obj.getString("sub_image"));
}
但我不知道如何获取 sub_image ,这是 item 之外的。如何实现这一目标?
答案 0 :(得分:1)
在这种情况下,您可以直接从JSONObject
获取字符串try {
JSONObject root = new JSONObject(items);
String subimage = root.getString("sub_image");
} catch(Exception ex){
}