Android 如何在以下结构中获取json数组中的值
我需要获取imagesall的值。如何做到这一点。下面是json结构Android 如何在以下结构中获取json数组中的值,android,json,Android,Json,我需要获取imagesall的值。如何做到这一点。下面是json结构 { "status": "true", "product": [ { "hostid": "65", "user_id": "39", "hometype": "Paying Guest", "roomtype": "Private Room"
{
"status": "true",
"product": [
{
"hostid": "65",
"user_id": "39",
"hometype": "Paying Guest",
"roomtype": "Private Room",
"imagesall": [
"http://hostguesthome.com/uploadedfile/hostImages/user_94Chrysanthemum.jpg",
"http://hostguesthome.com/uploadedfile/hostImages/user_15Desert.jpg",
"http://hostguesthome.com/uploadedfile/hostImages/user_13Hydrangeas.jpg"
]
},
到目前为止,我所做的如下,但它给出了错误。imagesall没有值。我试图在imagesall数组中找到第一个值
这是我的密码:
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray("product");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
JSONArray img_ar = c.getJSONArray("imagesall");
System.out.println("++++++"+img_ar.get(0).toString());
Log.i("This is DETAIL", ""+img_ar.get(0).toString());
你可以试试这个-
try {
JSONObject _jObject = new JSONObject("YOUR_JSON_STRING");
String _status = _jObject.getString("status");
JSONArray _jArray = _jObject.getJSONArray("product");
for (int i = 0; i < _jArray.length(); i++) {
JSONObject _jSubObj = _jArray.getJSONObject(i);
String _hostid = _jSubObj.getString("hostid");
String _user_id = _jSubObj.getString("user_id");
String _hometype = _jSubObj.getString("hometype");
String _roomtype = _jSubObj.getString("roomtype");
JSONArray _jArrImg = _jSubObj.getJSONArray("imagesall");
for (int j = 0; j < _jArrImg.length(); j++) {
String _img = _jArrImg.getString(j);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
试试看{
JSONObject_jObject=新的JSONObject(“您的JSON字符串”);
字符串_status=_jObject.getString(“status”);
JSONArray _jArray=_jObject.getJSONArray(“产品”);
对于(int i=0;i<_jArray.length();i++){
JSONObject _jSubObj=_jArray.getJSONObject(i);
String _hostid=_jSubObj.getString(“hostid”);
String _user_id=_jSubObj.getString(“user_id”);
字符串_hometype=_jSubObj.getString(“hometype”);
String _roomtype=_jSubObj.getString(“roomtype”);
JSONArray jArrImg=jSubObj.getJSONArray(“imagesall”);
对于(int j=0;j<_jArrImg.length();j++){
String _img=_jArrImg.getString(j);
}
}
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
尝试使用img_ar.getString(0)
仍然是相同的错误请发布您的全部代码。@shoeb。。它在这一行JSONArray img_ar=c.getJSONArray(“imagesall”)上给出错误;json在jsonlint上有效,结构与我发布的相同
try {
JSONObject _jObject = new JSONObject("YOUR_JSON_STRING");
String _status = _jObject.getString("status");
JSONArray _jArray = _jObject.getJSONArray("product");
for (int i = 0; i < _jArray.length(); i++) {
JSONObject _jSubObj = _jArray.getJSONObject(i);
String _hostid = _jSubObj.getString("hostid");
String _user_id = _jSubObj.getString("user_id");
String _hometype = _jSubObj.getString("hometype");
String _roomtype = _jSubObj.getString("roomtype");
JSONArray _jArrImg = _jSubObj.getJSONArray("imagesall");
for (int j = 0; j < _jArrImg.length(); j++) {
String _img = _jArrImg.getString(j);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}