解析json中的一些数组以及android中的一些项目

时间:2015-09-23 11:18:27

标签: android json

我想解析userinfo数组和其中的一些字符串。和getuserlistdata数组以及其中的一些字符串。请帮我解析这个问题。这是我的责任。

    {
        "status": "true",
        "data": {
            "userinfo": [
                {
                    "id": "77",
                    "firstname": "Test",
                }

              ],

    "getuserlistdata": [
                {

                    "address": "Kasauli, Himachal Pradesh, India"

    "hostImage": [
                        {
                            "id": "551",
                            "hostid": "122",
                            "images": "user_21t657.jpg",
                            "description": ""
                        },
                        {
                            "id": "3954",
                            "hostid": "122",
                            "images": "user_251541535.jpg",
                            "description": ""
                        },

                    ],

    ]

}
}
先生,这是我的代码。

protected Void doInBackground(Void...arg0) {
        // Creating service handler class instance
        ServiceHandler2 sh = new ServiceHandler2();

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

        Log.d("url_links: ", "> " + url_links);

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

        Log.v("Profile:", "> " + jsonStr);

        if (jsonStr != null) {

            try {
                JSONObject obj = new JSONObject(jsonStr);
                status = obj.getString("status");
                data = obj.getString("data");


                if (status.equalsIgnoreCase("false")) {

                } else {

                    jsonarr = obj.getJSONArray("userinfo");



                    for (int i = 0; i < jsonarr.length(); i++) {

                        JSONObject c = jsonarr.getJSONObject(i);
                        String fname = c.getString("firstname");
                        Datein_arr.add(fname);

                        String id = c.getString("id");
                    }

                    jsonarr2 = obj.getJSONArray("getuserlistdata");
                    for (int j = 0; j < jsonarr2.length(); j++) {
                        JSONObject jo = jsonarr2.getJSONObject(j);

                        address = jo.getString("address");

                        Log.v("address", "" + address);

                        Log.v("Profile:", "777777777777777777777777777777777777");



                        hostimgrr2 = obj.getJSONArray("hostImage");
                        if (hostimgrr2.length() == 0) {
                            ch_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");
                            det_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/user_12707aaf22_original.jpg");

                        } else {

                            for (int k = 0; k < hostimgrr2.length(); k++) {
                                JSONObject js = hostimgrr2.getJSONObject(k);

                                img = js.getString("images");

                                Log.v("img", "" + img);

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

                                ch_img.add("https://s3-ap-southeast-1.amazonaws.com/nanoweb/hostguesthome/uploadedfile/hostImages/" + img);

                            }

                        }

                    }

                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

首先,你必须验证你的jsondata。

您可以转到JSONLint来检查您的json是否有效

{
"status": "true",
"data": {
    "userinfo": [
        {
            "id": "77",
            "firstname": "Test"
        }
    ],
    "getuserlistdata": [
        {
            "address": "Kasauli, Himachal Pradesh, India",
            "hostImage": [
                {
                    "id": "551",
                    "hostid": "122",
                    "images": "user_21t657.jpg",
                    "description": ""
                },
                {
                    "id": "3954",
                    "hostid": "122",
                    "images": "user_251541535.jpg",
                    "description": ""
                }
            ]
        }
    ]
}
}

您正在将JSONObject解析为String

即,它应该是obj.getJSONObject("data");而不是obj.getString("data");

同时检查这样的其他解析问题。

答案 1 :(得分:0)

尝试使用以下代码来解析JSONArray

try {
    private ArrayList<UserInfo> userInfoList = new ArrayList<UserInfo>();
    JSONObject jsonObject = new JSONObject(jsonData); // jsonData is your reponse string

   JSONArray userinfo = jsonData.getJSONArray("userinfo");
        for(int i = 0; i < userinfo.length(); i++) {
            JSONObject jObject = userinfo.getJSONObject(i);
            UserInfo userInfo = new UserInfo();
            userInfo.setId( jObject.getString("id") );
            userInfo.setFirstname( jObject.getString("firstname") );
            userInfoList.add(userInfo);
        }       

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

UserInfo课程如下。

public class UserInfo {
    public String id                            = "";
    public String firstname                     = "";

    public String getID() {
        return coursePrice;
    }

    public void setID(String id) {
        this.id = id;
    }

    public String getFirstname() {
        return coursePrice;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}