简体   繁体   English

如何解析以下类型的json响应?

[英]how to parse the below type json response?

Hi i am completely new to android.please suggest me how to parse the below jsonresposne and get the values.any help please suggest me.i don't know how to parse because it contains both square bracket and curly bracket. 嗨,我是android的新手,请建议我如何解析以下jsonresposne并获取values.any帮助请建议我。我不知道如何解析,因为它既包含方括号又包含大括号。

 `[  
   {  
      "bus_schedule":[  
         {  
            "bus_route":[  
               {  
                  "serviceDate":"2016-12-31",
                  "amenities":" VB King Size Semi Sleeper (2 plus 1) selected WIFI,Water,Bottle,Blankets,Snacks,Movie,Food,Emergency exit, Fire Extinguisher,Bus Hostess,CCTV,Emergency Contact Number",
                  "startCityName":"Coimbatore",
                  "departureTime":"21:00:00",
                  "fare":"499",
                  "endCityName":"Kanyakumari",
                  "arrivalTime":"06:00:00",
                  "operatorName":"RUN TO WIN",
                  "bus_id":"17",
                  "journeyHours":"9",
                  "available_seat":25,
                  "total_seats":34,
                  "seat_type":"semi sleeper"
               }
            ],"boardingPoint":[  
               {  
                  "boardingPointName":"Thudiyalur",
                  "boardingPointContact":"8883088820",
                  "boardingPointTime":"21:25:00",
                  "boardingPointId":"316",
                  "BusId":"17"
               },`

            my code is 
                  `protected Void doInBackground(Void... params) {
                     HttpGet request = new HttpGet(url);
                   ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {
                response= mClient.execute(request, responseHandler);
            } catch (ClientProtocolException exception) {
                exception.printStackTrace();
                return null;
            } catch (IOException exception) {
                exception.printStackTrace();
                return  null;
            }
            Log.i("routes",""+response);
            jsonstr=response;

          if(jsonstr!= null){
              try{
                  /*JSONArray jsonary=new JSONArray(jsonstr);
                  JSONArray bus_scheduleee=jsonary.getJSONArray("bus_schedule");*/
                   JSONObject jsonObj = new JSONObject(jsonstr);
                  JSONArray bus_schedule = jsonObj.getJSONArray("bus_schedule");
                  JSONArray bus_route = jsonObj.getJSONArray("bus_route");
                  for (int i = 0; i < bus_route.length(); i++) {
                      JSONObject c = bus_route.getJSONObject(i);
                      String journeydatefromjson=c.getString("serviceDate");
                      String busname=c.getString("amenities");
                      String fromplace_json=c.getString("startCityName");
                      String departtimejson=c.getString("departureTime");
                      String farefromjson=c.getString("fare");
                      String endCityNamejson=c.getString("endCityName");
                      String arrivalTimejson=c.getString("arrivalTime");
                      String operatorNamejson=c.getString("operatorName");
                      String bus_idjson=c.getString("bus_id");
                      String journeyHoursjson=c.getString("journeyHours");
                      String available_seatjson=c.getString("available_seat");
                      String total_seatsjson=c.getString("total_seats");
                      String seat_typejson=c.getString("journeyHours");

                     Log.d("busdetails",""+journeydatefromjson+busname);
                  }

              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
            return null;
        }`

Try use Gson lib for parse it. 尝试使用Gson lib进行解析。

dependencies {
    compile 'com.google.code.gson:gson:2.3.1'
}

You can generation pojo for parse json. 您可以生成pojo来解析json。 (For example on this site http://www.jsonschema2pojo.org ) (例如,在此网站上http://www.jsonschema2pojo.org

For parse use this. 为了解析,使用这个。

T t = new Gson().fromJson(serverResponse.getResponse(), type);

Where 'T' is your response data, and 'type' it pojo from site. 其中“ T”是您的响应数据,然后从站点“输入”它。

If your type is entity use Type type = Entity.class; 如果您的类型是实体,请使用Type type = Entity.class; If your type is list of entities use 如果您的类型是实体列表,请使用

Type type = new TypeToken<List<WadadayFull>>() {
            }.getType())

try this code: 试试这个代码:

try {
            JSONArray array1 = new JSONArray(str);
            for (int i=0;i<array1.length();i++){
                JSONObject obj1 = array1.getJSONObject(i) ;
                JSONArray array2 = obj1.getJSONArray("bus_schedule");
                for (int j=0;j<array2.length();j++){
                    JSONObject obj2 = array2.getJSONObject(j);
                    JSONArray array3 = obj2.getJSONArray("bus_route");
                    ArrayList<Example1> list = new ArrayList<>();
                    for (int k=0;k<array3.length();k++) {
                        JSONObject obj3 = array3.getJSONObject(k);

                        String s1 = obj3.getString("serviceDate");
                        String s2 = obj3.getString("amenities");
                        String s3 = obj3.getString("startCityName");
                        String s4 = obj3.getString("departureTime");
                        String s5 = obj3.getString("fare");
                        String s6 = obj3.getString("endCityName");
                        String s7 = obj3.getString("arrivalTime");
                        String s8 = obj3.getString("operatorName");
                        String s9 = obj3.getString("bus_id");
                        String s10 = obj3.getString("journeyHours");
                        String s11 = obj3.getString("available_seat");
                        String s12 = obj3.getString("total_seats");
                        String s13 = obj3.getString("seat_type");

                        Example1 ex1 = new Example1();
                        ex1.setServiceDate(s1);
                        ex1.setAmenities(s2);
                        ex1.setStartCityName(s3);

                        list.add(ex1);
                        Log.e("response", list.toString());


                    }

                    JSONArray array4 = obj2.getJSONArray("boardingPoint");
                    ArrayList<Example2> list2 = new ArrayList<>();
                    for (int l = 0; l < array4.length(); l++) {
                        JSONObject obj4 = array4.getJSONObject(l);

                        String s14 = obj4.getString("boardingPointName");
                        String s15 = obj4.getString("boardingPointContact");
                        String s16 = obj4.getString("boardingPointTime");
                        String s17 = obj4.getString("boardingPointId");
                        String s18 = obj4.getString("BusId");
                        Example2 ex2 = new Example2();
                        ex2.setBoardingPointName(s14);
                        ex2.setBoardingPointContact(s15);
                        list2.add(ex2);
                        Log.e("response", list2.toString());
                    }

                }

            }

            Log.e("response",array1.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM