如何在android中获取嵌套的json数组项

时间:2015-09-25 06:33:20

标签: android json

这是我的json响应,我想从(hostImage)数组获取(getuserlistdata)数组项和图像。请帮我在doInBackground中解析这个响应。

{
    "status": "true",
    "data": {
        "userinfo": [
            {
                "id": "77",

                "auth_key": "",
                "password_hash": "ceb6c970658f31504a901b89dcd3e461",
                "password_reset_token": null,
                "firstname": "Test",
                "lastname": "",
                "phone_verify": ""
            }
        ],
        "getuserlistdata": [
            {
                "hostid": "122",
                "user_id": "77",
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "551",
                        "hostid": "122",
                        "images": "user_21t657.jpg",
                        "description": ""
                    },
                    {
                        "id": "3954",
                        "hostid": "122",
                        "images": "user_251541535.jpg",
                        "description": ""
                    },
                    {
                        "id": "3955",
                        "hostid": "122",
                        "images": "user_69beautiful-city-wallpaper-.jpg",
                        "description": ""
                    },
                    {
                        "id": "3959",
                        "hostid": "122",
                        "images": "user_85download.jpg",
                        "description": ""
                    },
                    {
                        "id": "3960",
                        "hostid": "122",
                        "images": "user_65images.jpg",
                        "description": ""
                    },
                    {
                        "id": "8925",
                        "hostid": "122",
                        "images": "user_15exclusive-well-designed-free-blue-living-room-hd.jpg",
                        "description": ""
                    },
                    {
                        "id": "8926",
                        "hostid": "122",
                        "images": "user_59luxury-rooms-desings-wallpapers.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "phone_verify": ""
                }
            },
            {
                "hostid": "500",
                "user_id": "77",
                "state": " Himachal Pradesh",
                "address": "Tutikandi, Shimla, Himachal Pradesh, India",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "1546",
                        "hostid": "500",
                        "images": "user_85435.jpg",
                        "description": ""
                    },
                    {
                        "id": "1547",
                        "hostid": "500",
                        "images": "user_376929237.jpg",
                        "description": ""
                    },
                    {
                        "id": "1548",
                        "hostid": "500",
                        "images": "user_7512699433.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "phone_verify": ""
                }
            },
            {
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "1727",
                        "hostid": "569",
                        "images": "user_81435.jpg",
                        "description": ""
                    },
                    {
                        "id": "1728",
                        "hostid": "569",
                        "images": "user_6912699433.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "address": "chandigarh india",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "607",
                "user_id": "77",
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "1913",
                        "hostid": "607",
                        "images": "user_64435.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "609",
                "address": "Leh Manali Hwy, Himachal Pradesh 175140, India",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "1958",
                        "hostid": "609",
                        "images": "user_57435.jpg",
                        "description": ""
                    },
                    {
                        "id": "8971",
                        "hostid": "609",
                        "images": "user_13Home-Interior-Design-Hd-Wallpaper-Hd-Background-With-Simple-Staircase-And-Plasma-TV-Also-Nice",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2453",
                "user_id": "77",
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "9658",
                        "hostid": "2453",
                        "images": "user_92home-interior-design-ideas-1920x1200-on-interiorpicture.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2517",
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "10002",
                        "hostid": "2517",
                        "images": "user_787865-most-beautiful-places-in-the-world-hd-wallpaper-1600x900.jpg",
                        "description": ""
                    },
                    {
                        "id": "11441",
                        "hostid": "2517",
                        "images": "user_47apeiron-hotel-dubai.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "lastname": "",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2582",
                "user_id": "77",
                "hometype": "Villa",
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "10955",
                        "hostid": "2582",
                        "images": "user_15lets-travel-to-united-arab-emirates-abu-dhabi-with-sanjay-pradhan-2.jpg",
                        "description": ""
                    },
                    {
                        "id": "10957",
                        "hostid": "2582",
                        "images": "user_30UAE-Real-estate-dubai.jpg",
                        "description": ""
                    },
                    {
                        "id": "10958",
                        "hostid": "2582",
                        "images": "user_19Wonderful-living-rooms-Elegant-Wallpaper-HD.jpg",
                        "description": ""
                    },
                    {
                        "id": "10959",
                        "hostid": "2582",
                        "images": "user_13wood-bathroom-design-and-interior-home-design-photo.jpg",
                        "description": ""
                    },
                    {
                        "id": "10960",
                        "hostid": "2582",
                        "images": "user_1029983-325892.jpg",
                        "description": ""
                    },
                    {
                        "id": "10961",
                        "hostid": "2582",
                        "images": "user_72apeiron-hotel-dubai.jpg",
                        "description": ""
                    },
                    {
                        "id": "10962",
                        "hostid": "2582",
                        "images": "user_89Dubai-UAE-Wallpaper.jpg",
                        "description": ""
                    },
                    {
                        "id": "10963",
                        "hostid": "2582",
                        "images": "user_83luxury-rooms-desings-wallpapers.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "auth_key": "",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2619",
                "user_id": "77",
                "hometype": "Villa",
                "roomtype": "Entire Home",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "10519",
                        "hostid": "2619",
                        "images": "user_131541535.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "auth_key": "",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2667",
                "user_id": "77",
                "hometype": "Villa",
                "roomtype": "Entire Home",
                "address": "3A, Amparafaravola, Madagascar",
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "11157",
                        "hostid": "2667",
                        "images": "user_20DCP_3742.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "auth_key": "",
                    "password_hash": "ceb6c970658f31504a901b89dcd3e461",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "lastname": "",
                    "phone_verify": ""
                }
            },
            {
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "11159",
                        "hostid": "2744",
                        "images": "user_237865-most-beautiful-places-in-the-world-hd-wallpaper-1600x900.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2745",
                "address": "763 Purerua Rd, Te Tii 0294, New Zealand",
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "11160",
                        "hostid": "2745",
                        "images": "user_171541535.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2759",
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "11229",
                        "hostid": "2759",
                        "images": "user_2229983-325892.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "phone_verify": ""
                }
            },
            {
                "hostid": "2813",
                "cancellationpolicies": "Moderate",
                "hostImage": [
                    {
                        "id": "11841",
                        "hostid": "2813",
                        "images": "user_89green-kitchen-home-interior-designs.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2863",
                "user_id": "77",
                "hometype": "Villa",
                "cancellationpolicies": "Strict",
                "hostImage": [
                    {
                        "id": "11742",
                        "hostid": "2863",
                        "images": "user_86DCP_3742.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "phone_verify": ""
                }
            },
            {
                "hostid": "2886",
                "user_id": "77",
                "hometype": "Apartment",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "11843",
                        "hostid": "2886",
                        "images": "user_100exclusive-well-designed-free-blue-living-room-hd.jpg",
                        "description": ""
                    },
                    {
                        "id": "11844",
                        "hostid": "2886",
                        "images": "user_100green-kitchen-home-interior-designs.jpg",
                        "description": ""
                    },
                    {
                        "id": "11845",
                        "hostid": "2886",
                        "images": "user_22Home-Decorating-41.jpg",
                        "description": ""
                    },
                    {
                        "id": "11846",
                        "hostid": "2886",
                        "images": "user_61home-design-interior-luxury-light-color-bed-room-785x488.jpg",
                        "description": ""
                    },
                    {
                        "id": "11847",
                        "hostid": "2886",
                        "images": "user_22wood-bathroom-design-and-interior-home-design-photo.jpg",
                        "description": ""
                    },
                    {
                        "id": "11848",
                        "hostid": "2886",
                        "images": "user_3626_591.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2945",
                "user_id": "77",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "12172",
                        "hostid": "2945",
                        "images": "user_71top-home-interior-designer-06.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2961",
                "user_id": "77",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "12272",
                        "hostid": "2961",
                        "images": "user_487865-most-beautiful-places-in-the-world-hd-wallpaper-1600x900.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2963",
                "user_id": "77",
                "hometype": "house",
                "roomtype": "shared Room",
                "accommodates": "3",
                "cancellationpolicies": "Flexible",
                "hostImage": [],
                "user": {
                    "id": "77",

                    "phone_verify": ""
                }
            },
            {
                "hostid": "2964",
                "user_id": "77",
                "hometype": "house",
                "roomtype": "shared Room",
                "accommodates": "3",
                "cancellationpolicies": "Flexible",
                "hostImage": [],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "password_hash": "ceb6c970658f31504a901b89dcd3e461",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "lastname": "",
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2967",
                "user_id": "77",
                "hometype": "house",
                "roomtype": "Private Room",
                "cancellationpolicies": "Flexible",
                "hostImage": [],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "password_hash": "ceb6c970658f31504a901b89dcd3e461",
                    "password_reset_token": null,
                    "phone_verify": ""
                }
            },
            {
                "hostid": "2968",
                "user_id": "77",
                "hometype": "Villa",
                "roomtype": "shared Room",
                "cancellationpolicies": "Flexible",
                "hostImage": [
                    {
                        "id": "12278",
                        "hostid": "2968",
                        "images": "user_15Koala.jpg",
                        "description": ""
                    }
                ],
                "user": {
                    "id": "77",

                    "auth_key": "",
                    "password_hash": "ceb6c970658f31504a901b89dcd3e461",
                    "password_reset_token": null,
                    "firstname": "Test",
                    "phone_verify": ""
                }
            }
        ],
        "fetch_all_ref": [],
        "tripdata": []
    }
}

这是我的安卓代码。

    protected Void doInBackground(Void... params) {

        ServiceHandler2 sh = new ServiceHandler2();

        String url="http://192.168.0.65/hostandguest/android/viewprofile?uid=77";

        String jsonStr = sh.makeServiceCall(url, ServiceHandler2.GET);



        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {

            try {

                JSONObject jsonObj = new JSONObject(jsonStr);
                status = jsonObj.getString("status");
                Userarr=jsonObj.getJSONObject("data").getJSONArray("userinfo");



                if (status.equalsIgnoreCase("false")) {
                    Log.v("PostExecute", "1111111111111111111111");

                } else {
                    // Getting JSON Array node
                    Userarr=jsonObj.getJSONObject("data").getJSONArray("userinfo");

                    // looping through All Contacts
                    for (int i = 0; i < Userarr.length(); i++) {

                        JSONObject c = Userarr.getJSONObject(i);


                        String Fname=c.getString("firstname");
                        Fname_arr.add(Fname);
                        Log.v("Fname",Fname);






                        JSONArray _jArrImg = c.getJSONArray("getuserlistdata");






                            for (int j = 0; j < _jArrImg.length(); j++) {
                                JSONObject js = _jArrImg.getJSONObject(j);

                                JSONArray hostimage_arr = c.getJSONArray("hostImage");
                                for(int k = 0; k < hostimage_arr.length(); k++)
                                {
                                    String image = js.getString("images");

                                    if (_jArrImg.length() ==0) {

                                        det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");

                                    }


                                    if (j==0) {
                                        det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/"+image);
                                    }

                                }








                        }




                    }



                }










            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

5 个答案:

答案 0 :(得分:0)

这是你的问题

for(int k = 0; k < hostimage_arr.length(); k++)
{
    //Add this line
    JsonObject innerObj=hostimage_arr.getJsonObject(k);
    String image = js.getString("images");   // change it to innerObj.getString("images");
    if (_jArrImg.length() ==0) {
         det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");

    }


    if (j==0) {
         det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/"+image);
    }

}

答案 1 :(得分:0)

您的第二个jsonarray(getuserlistdata)位于数据对象下。但你试图从userinfo jsonarray获取。删除usserarr的循环它将解决您的问题。

 Userarr=jsonObj.getJSONObject("data").getJSONArray("userinfo");

                // looping through All Contacts
  

删除循环

              for (int i = 0; i < Userarr.length(); i++) {
                    JSONObject c = Userarr.getJSONObject(i);

                    String Fname=c.getString("firstname");
                    Fname_arr.add(Fname);
                    Log.v("Fname",Fname);

            //change this

             JSONArray _jArrImg = jsonObj.getJSONObject("data").getJSONArray("getuserlistdata");

       for (int j = 0; j < _jArrImg.length(); j++) {
                            JSONObject js = _jArrImg.getJSONObject(j);

                            JSONArray hostimage_arr = c.getJSONArray("hostImage");
                            for(int k = 0; k < hostimage_arr.length(); k++)
                            {
                  // you are try get image from js object create a new object
                               JSONObject hostimage = hostimage_arr.getJSONObject(k);
                                String image = hostimage.getString("images");

                                if (_jArrImg.length() ==0) {

                                    det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");

                                }


                                if (j==0) {
                                    det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/"+image);
                                }

                            }

答案 2 :(得分:0)

使用您的代码更改此代码:

问题:您尝试从"images"对象获取js

JSONArray hostimage_arr = c.getJSONArray("hostImage");
        for (int k = 0; k < hostimage_arr.length(); k++) {
            JSONObject jImage = _jArrImg.getJSONObject(j); // Add this line
            String image = jImage.getString("images");  // Update this line

            if (_jArrImg.length() == 0) {

                det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");

            }

            if (j == 0) {
                det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/"
                        + image);
            }
        }

答案 3 :(得分:0)

您需要制作不同的jsonobject,以便从您的回复中获取不同的数组。

JSONObject jsonObjgetuserlistdata = response.getjObj();
JSONObject jobj , iobjimage;

JSONArray jsonQueryArray = new JSONArray(jsonObjQuery .getString("getuserlistdata"));
JSONArray jsonTeacherArray = new JSONArray(jsonObjTeacher .getString("teacher"));

    for (int i = 0; i < jsonObjgetuserlistdata .length(); i++) {
            jobj = jsonObjgetuserlistdata .getJSONObject(i);
            JSONArray jsonImageArray = new JSONArray(jobj .getString("hostImage"));

                        for (int j = 0; j < jsonImageArray .length(); j++) {
                                iobjimage= jsonQuestionArray.getJSONObject(j);
                                image = iobjimage.getString("images"
                            }
    }

答案 4 :(得分:0)

使用Gson库。 请检查以下链接 https://github.com/google/gson

为此你只需要为你的回复创建java pojo。

并调用以下方法:

new Gson()。fromJson(userString,YourResponsePojo.class) 这将返回您的响应对象

相关问题