Volley JSON PARSING错误json.JSONException:索引1超出范围[0..1)

时间:2018-04-20 10:47:52

标签: java android arrays json object

我无法获取JSONARRAY。目前,我正在发送一个请求,它向我发回包含数组和对象的响应。这个数组里面有一个数组,我无法从中获取数据,因为如果索引1超出范围[0 ... 1]

,它会给出错误
`{
"ItemDetails": [
    {
        "LocationName": "JLT A Cluster",
        "Products": [
            {
                "Barcode": "5760466743686",
                "MainGroupName": "Chilled",
                "OnHandQty": 0,
                "Price": 10.5,
                "ProductName": "Mozzarella Portions",
                "SubGroupName": "Cheese Fats Butter"
            }
        ]
    }
],
"Status": {
    "Error": "",
    "ResultCode": "1",
    "ResultStatus": "Success"
}
}`

这是我的代码

 public void onResponse(JSONObject response) {

                        Log.d("APPPPPPPPPPPPP", response.toString());
                        try {
                            JSONObject status = response.getJSONObject("Status");
                            int Resultcode = status.getInt("ResultCode");
                            Log.d("",""+status.getString("ResultCode").toString());

                            if(Resultcode == 1){
                                JSONArray ItemDetail = response.getJSONArray("ItemDetails");
                                String LocationName = ItemDetail.getJSONObject(0).getString("LocationName");
                                   JSONArray Products = ItemDetail.getJSONArray(1);
                             //   JSONObject info = Products.getJSONObject(0);
                                   Log.d("HEYYYYY",""+Products);

                                //String ame = ItemDetail.getJSONArray(1).getJSONObject(0).getString("Barcode");



                            }else if(Resultcode == 109){

                            }



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //tv.setText(response.toString());

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("EROOOOOOOOOOOOOOOOOOOOOR", "Error: " + error.getMessage());
            }

我可以获取atleaset位置,并且可以轻松地从状态中获取所有数据,但无法从产品内部获取。

1 个答案:

答案 0 :(得分:1)

而不是这一行

 JSONArray Products = ItemDetail.getJSONArray(1);

尝试添加此

JSONArray Products = ItemDetail.getJSONObject(0).getJSONArray("Products");
相关问题