从复杂的JSON响应中检索Android中的嵌套JSON数组/对象

时间:2015-05-27 10:31:48

标签: android json arraylist android-listview nested-loops

我必须将下面嵌套的JSON数组的数据解析到我的应用程序中。我很困惑如何从中获取值。因为它太复杂了网址是“http://gaadiready.com/flight_api/getFlightSearchResults/?key=PPSYrvnMnS&trip_type=domestic&flight_mode=ONE&from=Bangalore/BLR&to=Calicut/CCJ&depart_date=05-06-2015&return_date=06-06-2015&no_adult=1&no_child=0&no_infant=0&cabin_type=E

JSON响应由嵌套数组和对象组成。我使用Volley库函数来解析JSON。我想从响应中获取值以生成列表视图。但我没有得到输出。任何人帮我解决这个问题。我不熟悉嵌套的JSON数组。

{"tag":"Flight SearchResults","success":"1","xml_filename":"dom_20150527135409.xml",

"data":

[{"depart_from_location":"Bangalore","depart_from_location_code":"BLR","depart_to_location":"Calicut","depart_to_location_code":"CCJ","depart_date":"Fri, 05 June 2015","return_from_location":"Calicut","return_from_location_code":"CCJ","return_to_location":"Bangalore","return_to_location_code":"BLR","return_date":"Sat, 06 June 2015",

"departure_details":[{"flight_id":"arzoo11","flights_count":"2",

"flight_details":

[{"image_filename":"http:\/\/live.arzoo.com\/FlightWS\/image\/AirIndia.gif","air

line_name":"Air India","flight_number":"608","departure_location_code":"BLR","arrival_location_code":"BOM","departure_location":"Bangalore ","departure_time":"21:20","arrival_location":"Mumbai","arrival_time":"23:00","fare_rules":"This fare is Non Refundable Booking Class : T|Cancellation Penalty: All bookings done are subject to cancellation penalty levied by the airlines.<br>In addition to the airlines cancellation penalty, we charge a service fee of Rs. 50 per passenger for all cancellations.|Date Change Penalty: In addition to the airlines date change penalty, we charge a service fee of Rs. 50 per passenger.|"},{"image_filename":"http:\/\/live.arzoo.com\/FlightWS\/image\/AirIndia.gif","airline_name":"Air India","flight_number":"657","departure_location_code":"BOM","arrival_location_code":"CCJ","departure_location":"Mumbai","departure_time":"10:00","arrival_location":"Calicut","arrival_time":"13:05","fare_rules":"This fare is Non Refundable Booking Class : T|Cancellation Penalty: All bookings done are subject to cancellation penalty levied by the airlines.<br>In addition to the airlines cancellation penalty, we charge a service fee of Rs. 50 per passenger for all cancellations.|Date Change Penalty: In addition to the airlines date change penalty, we charge a service fee of Rs. 50 per passenger.|"}],

"fare_details":

{"base_fare":"7301","airport_tax":"5365","service_tax":"46","transaction_charge":"0","total":12962,"total_amount":"12962.00"}},

我到目前为止尝试过的代码如下所示,我正在使用凌空进行解析

private void json_parser(JSONArray jarray){
  try  {

        flightList=new ArrayList<Flight>();
      for (int i = 0; i < jarray.length(); i++) {

            JSONObject object = jarray.getJSONObject(i);

          JSONArray departure_details=object.getJSONArray("departure_details");
          if(departure_details!=null)
          {
              for(int j=0;j<departure_details.length();j++)
              {
                  JSONObject object2=departure_details.getJSONObject(j);
                  String flight_count=object2.getString("flight_count");


            JSONArray flight_details=object2.getJSONArray("flight_details");
                  if(flight_details!=null)
                  {
                      for(int k=0;k<flight_details.length();k++)
                      {
    JSONObject object3=flight_details.getJSONObject(k);
                          String flight_name=object3.getString("airline_name");
                          String dep_location_code=object3.getString("departure_location_code");
                          String arr_location_code=object3.getString("arrival_location_code");
    String dep_time=object3.getString("departure_time");
                          String arr_time=object3.getString("arrival_time");

          JSONObject fare_details=object.getJSONObject("fare_details");
                          Toast.makeText(getApplicationContext(),object.getString("depart_from_location_code"), Toast.LENGTH_LONG).show();
           flightList.add(new Flight( object.getString("depart_from_location_code"),object.getString("depart_to_location_code"),flight_count,flight_name,object.getString("depart_date"),object.getString("depart_date"),dep_time,arr_time,dep_location_code,arr_location_code,dep_location_code,arr_location_code));

        }}}}}
        list.setAdapter( new FlightListAdapter(ctx, R.layout.flight_list, flightList) ); 

1 个答案:

答案 0 :(得分:4)

private void json_parser(JSONArray jarray) {
    try {

        ArrayList flightList = new ArrayList<Flight>();
        for (int i = 0; i < jarray.length(); i++) {

            JSONObject object = jarray.getJSONObject(i);

            JSONArray departure_details = object
                    .getJSONArray("departure_details");
            if (departure_details != null) {
                for (int j = 0; j < departure_details.length(); j++) {
                    JSONObject object2 = departure_details.getJSONObject(j);

                    String flight_count = object2
                            .getString("flights_count");

                    JSONObject fare_details = object2
                            .getJSONObject("fare_details");

                    String base_fare, airport_tax, service_tax, transaction_charge, total, total_amount;

                    Log.i("fare_details",
                            "fare_details:" + fare_details.toString());
                    for (int k = 0; k < fare_details.length(); k++) {
                        base_fare = fare_details.getString("base_fare");
                        airport_tax = fare_details.getString("airport_tax");
                        service_tax = fare_details.getString("service_tax");
                        transaction_charge = fare_details
                                .getString("transaction_charge");
                        total = fare_details.getString("total");
                        total_amount = fare_details
                                .getString("total_amount");

                    }

                    JSONArray flight_details = object2
                            .getJSONArray("flight_details");
                    if (flight_details != null) {
                        Log.i("flight_details", "flight_details:"
                                + flight_details.toString());

                        for (int k = 0; k < flight_details.length(); k++) {
                            JSONObject object3 = flight_details
                                    .getJSONObject(k);
                            String flight_name = object3
                                    .getString("airline_name");
                            String dep_location_code = object3
                                    .getString("departure_location_code");
                            String arr_location_code = object3
                                    .getString("arrival_location_code");
                            String dep_time = object3
                                    .getString("departure_time");
                            String arr_time = object3
                                    .getString("arrival_time");

                            flightList
                                    .add(new Flight(
                                            object.getString("depart_from_location_code"),
                                            object.getString("depart_to_location_code"),
                                            flight_count,
                                            flight_name,
                                            object.getString("depart_date"),
                                            object.getString("depart_date"),
                                            dep_time, arr_time,
                                            dep_location_code,
                                            arr_location_code,
                                            dep_location_code,
                                            arr_location_code));
                        }

                    }

                }
            }
        }
        Log.i("array list length", "" + flightList.size());

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

}