Android:从嵌套在另一个数组中的数组中检索JSON对象

时间:2018-03-18 07:34:45

标签: android arrays json arraylist android-volley

我正在尝试从另一个数组中的数组中获取JSON对象。请参阅下面的JSON:

{
"status": 0,
"message": "successfull",
"resp_body": [
    {
        "route_id": "4567657tyrthnhrthnyt65y",
        "distance": "200km",
        "route_duration": "2hrs",
        "express": true,
        "status": "active",
        "route_name": "nairobi-nyeri",
        "peak_fare": "250",
        "offpeak_fare": "200",
        "trips_assigned": [
            {
                "trip_id": "43tergf",
                "date": "2/2/16",
                "status": "enroute"
            },
            {
                "trip_id": "45i27563t34",
                "date": "12/12/12",
                "status": "finished"
            },
            {
                "trip_id": "365ttrg6",
                "date": "31/12/17",
                "status": "queded"
            }
        ],
        "vehicles_ assigned": [
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            }
        ],
        "origin": [
            {
                "town": "nairobi",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "destination": [
            {
                "town": "nyeri",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "via": [
            {
                "town": "sagana",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            },
            {
                "town": "karatina",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ]
    },
    {
        "route_id": "erdbgthrr",
        "distance": "230km",
        "route_duration": "2hrs",
        "express": false,
        "route_name": "nairobi-sagana-karatina-nyeri",
        "status": "active",
        "peak_fare": "250",
        "offpeak_fare": "200",
        "trips_assigned": [
            {
                "trip_id": "43tergf",
                "date": "2/2/16",
                "status": "enroute"
            },
            {
                "trip_id": "45i27563t34",
                "date": "12/12/12",
                "status": "finished"
            },
            {
                "trip_id": "365ttrg6",
                "date": "31/12/17",
                "status": "queded"
            }
        ],
        "vehicles_ assigned": [
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            }
        ],
        "origin": [
            {
                "town": "nairobi",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "destination": [
            {
                "town": "nyeri",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "via": [
            {
                "town": "sagana",
                "stage": "tea_room",
                "latitude": "12.35454545",
                "longitude": "9.68685578",
                "arrival_time": "3:14pm"
            },
            {
                "town": "karatina",
                "stage": "tea_room",
                "latitude": "12.35454545",
                "longitude": "9.68685578",
                "arrival_time": "3:14pm"
            }
        ]
    },
    {
        "route_id": "rtbgfnhr5t",
        "route_name": "nairobi-thika",
        "distance": "200km",
        "route_duration": "2hrs",
        "express": false,
        "status": "active",
        "peak_fare": "250",
        "offpeak_fare": "200",
        "trips_assigned": [
            {
                "trip_id": "43tergf",
                "date": "2/2/16",
                "status": "enroute"
            },
            {
                "trip_id": "45i27563t34",
                "date": "12/12/12",
                "status": "finished"
            },
            {
                "trip_id": "365ttrg6",
                "date": "31/12/17",
                "status": "queded"
            }
        ],
        "trip_progress": [
            {
                "current location": "nyeri",
                "latitude": "32.54554654",
                "longitude": "-1.4565657",
                "average_speed": "67.89 km/h",
                "remaining_kms": "0",
                "arrival_time": "4.00am",
                "trip_duration": "76mins"
            }
        ],
        "origin": [
            {
                "name": "nairobi",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "destination": [
            {
                "name": "nyeri",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "via": [
            {
                "name": "karatina",
                "latitude": "12.35454545",
                "longitude": "9.68685578",
                "arrival_time": "3:14pm"
            }
        ]
    },
    {
        "route_id": "rtbgfnhr5t",
        "route_name": "nairobi-thika",
        "distance": "200km",
        "route_duration": "2hrs",
        "express": false,
        "status": "active",
        "peak_fare": "250",
        "offpeak_fare": "200",
        "trips_assigned": [
            {
                "trip_id": "43tergf",
                "date": "2/2/16",
                "status": "enroute"
            },
            {
                "trip_id": "45i27563t34",
                "date": "12/12/12",
                "status": "finished"
            },
            {
                "trip_id": "365ttrg6",
                "date": "31/12/17",
                "status": "queded"
            }
        ],
        "vehicles_ assigned": [
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            },
            {
                "vehicle_id": "nai234",
                "date": "12/12/12"
            }
        ],
        "origin": [
            {
                "town": "nairobi",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "destination": [
            {
                "town": "thika",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "via": []
    },
    {
        "departure_time": "2:00 pm",
        "e_t_a": "4:00 am",
        "express": false,
        "status": "finished",
        "date": "23/12/18",
        "route_name": "nairobi-thika",
        "trip_id": "12:00 am",
        "vehicle_id": "7HGF68",
        "trips_assigned": [
            {
                "trip_id": "43tergf",
                "date": "2/2/16",
                "status": "enroute"
            },
            {
                "trip_id": "45i27563t34",
                "date": "12/12/12",
                "status": "finished"
            },
            {
                "trip_id": "365ttrg6",
                "date": "31/12/17",
                "status": "queded"
            }
        ],
        "trip_progress": [
            {
                "current location": "nyeri",
                "latitude": "32.54554654",
                "longitude": "-1.4565657",
                "average_speed": "67.89 km/h",
                "remaining_kms": "0",
                "arrival_time": "4.00am",
                "trip_duration": "76mins"
            }
        ],
        "origin": [
            {
                "town": "thika",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "destination": [
            {
                "town": "thika",
                "stage": "tea_room",
                "latitude": "32.54554654",
                "longitude": "-1.4565657"
            }
        ],
        "via": [
            {
                "town": "thika",
                "stage": "tea_room",
                "latitude": "12.35454545",
                "longitude": "9.68685578",
                "arrival_time": "3:14pm"
            }
        ]
    }
]}

在上面的JSON主体中,我在

中有一系列路由
  

“resp_body”

在每条路线中,我都有一个

数组
  

“trips_assigned”

我的目标是列出ListView中的路线,并在点击路线时,检索该路线中的所有trip_assigned。我的代码能够做到这一点,但会抛出错误:

  

索引3超出范围[0..3)W / System.err:at   org.json.JSONArray.get(JSONArray.java:293)W / System.err:at   org.json.JSONArray.getJSONObject(JSONArray.java:521)

单击列表中的第四个路径时。

我的Java代码:

if ( jsonObject.getInt(getString(R.string.status)) == SUCCESS ) {
            respBody = jsonObject.getJSONArray(getString(R.string.resp_body));
            ArrayList<HashMap<String, String>> routeList = new ArrayList<HashMap<String, String>>();
            for(int i=0; i < respBody.length(); i++) {
                HashMap<String, String> temp = new HashMap<String, String>();
                temp.put("route_name", respBody.getJSONObject(i).getString(getString(R.string.route_name)));
                temp.put("route_distance", respBody.getJSONObject(j).getString(getString(R.string.status)));
                temp.put("peak_fare", respBody.getJSONObject(i).getString(getString(R.string.peak_fare)));
                temp.put("off_peak_fare", respBody.getJSONObject(i).getString(getString(R.string.offpeak_fare)));
                routeList.add(temp);

                tripsAssigned = (JSONArray) respBody.getJSONObject(i).get(getString(R.string.trips_assigned));
                for(j=0; j < tripsAssigned.length(); j++) {
                    perResult = respBody.getJSONObject(i);
                    noRecords.setVisibility(View.GONE);
                }

                RouteViewAdapter adapter = new RouteViewAdapter(getActivity(), routeList);
                if(routeList.size() < 1){
                    noRecords.setText("No routes to show.");
                } else {
                    listView.setVisibility(View.VISIBLE);
                    noRecords.setVisibility(View.GONE);
                }
                listView.setAdapter(adapter);
                pd.dismiss();
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                        try {


                            String t1 = perResult.getJSONArray(getString(R.string.trips_assigned)).getJSONObject(position).getString(getString(R.string.date));
                            String t2 = perResult.getJSONArray(getString(R.string.trips_assigned)).getJSONObject(position).getString(getString(R.string.status));
                            Toast.makeText(getActivity(), t1+t2, Toast.LENGTH_LONG).show();

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


                    }
                });

            }
            pd.dismiss();
        }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个。我希望这会对你有所帮助

   private void parseJson(JSONObject response) throws JSONException {

    JSONArray respBodyArray = response.optJSONArray("resp_body");
    if (respBodyArray != null){

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

            JSONObject arrayObject = (JSONObject) respBodyArray.get(i);

            String routeId = arrayObject.getString("route_id");

            JSONArray tripsAssignedArray = arrayObject.optJSONArray("trips_assigned");

            for (int j=0; j<tripsAssignedArray.length(); j++){

                JSONObject tripsAssignedArrayObject = (JSONObject) tripsAssignedArray.get(j);

                String tripId = tripsAssignedArrayObject.getString("trip_id");

            }

        }

    }

}