如何在以下结构中获取json数组中的值

时间:2015-06-24 10:35:15

标签: android json

我需要获取imagesall的值。怎么做这是json结构

 {
        "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());

错误日志是:

06-24 16:13:45.476: W/System.err(14536): org.json.JSONException: No value for imagesall
06-24 16:13:45.476: W/System.err(14536):    at org.json.JSONObject.get(JSONObject.java:355)
06-24 16:13:45.477: W/System.err(14536):    at org.json.JSONObject.getJSONArray(JSONObject.java:549)
06-24 16:13:45.477: W/System.err(14536):    at com.example.hostguestapp.Details$Getdetails.doInBackground(Details.java:101)
06-24 16:13:45.478: W/System.err(14536):    at com.example.hostguestapp.Details$Getdetails.doInBackground(Details.java:1)
06-24 16:13:45.478: W/System.err(14536):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-24 16:13:45.478: W/System.err(14536):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 16:13:45.478: W/System.err(14536):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-24 16:13:45.478: W/System.err(14536):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-24 16:13:45.479: W/System.err(14536):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-24 16:13:45.479: W/System.err(14536):    at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:0)

你可以试试这个 -

            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();
            }