解析JSON以使用Asynctask获取所有细节

时间:2017-09-18 19:09:22

标签: java android arrays json

{

  "response_code": 200,

  "debit": 3,


  "position": "Train has reached Destination and late by 5 minutes",

  "train": {

    "number": "12046",
    "name": "CDG NDLS SHTBDI"
  },

  "route": [
            {

"no": 1,

      "day": 0,

      "station": {
        "name": "CHANDIGARH",
        "code": "CDG"
      },
      "has_arrived": false,
      "has_departed": true,
      "distance": 0,
      "scharr": "Source",
      "schdep": "12:00",
      "actarr": "00:00",
      "actdep": "12:00",
      "scharr_date": "19 Nov 2015",
      "actarr_date": "19 Nov 2015",
      "latemin": 0
    },
    {
      "no": 2,
      "day": 0,
      "station": {
        "name": "AMBALA CANT JN",
        "code": "UMB"
      },
      "has_arrived": true,
      "has_departed": true,
      "distance": 67,
      "scharr": "12:40",
      "schdep": "12:42",
      "actarr": "12:40",
      "actdep": "12:42",
      "scharr_date": "19 Nov 2015",
      "actarr_date": "19 Nov 2015",
      "latemin": 0
    },
    {
      "no": 3,
      "day": 0,
      "station": {
        "name": "NEW DELHI",
        "code": "NDLS"
      },
      "has_arrived": true,
      "has_departed": false,
      "distance": 265,
      "scharr": "15:25",
      "schdep": "Destination",
      "actarr": "15:30",
      "actdep": "00:00",
      "scharr_date": "19 Nov 2015",
      "actarr_date": "19 Nov 2015",
      "latemin": 5
    }
  ]
}

在这里输入代码

public class TrainStatus {

    public static int responseCode;
    private int serialNo;
    private String scheduleArrival;
    private String actualArrival;
    public static String station;
    public static int debit;
    private String statusOfArrival;
    private boolean hasArrived;
    private boolean hasDeparted;
    private int latemin;
    private String actualArrivalDate;
    private String scheduleArrivalDate;
    public static String position;
    public  String name;

        public String getName()
            {
                return getName();
            }
        public void setName(String name)
            {
                this.name=name;
            }

        public boolean isHasArrived()
            {
                return hasArrived;
            }

        public void setHasArrived(boolean hasArrived)
            {
                this.hasArrived = hasArrived;
            }

        public boolean isHasDeparted()
            {
                return hasDeparted;
            }

        public void setHasDeparted(boolean hasDeparted)
            {
            this.hasDeparted = hasDeparted;
            }

        public int getLatemin()
        {
        return latemin;
        }

        public void setLatemin(int latemin) 
        {
        this.latemin = latemin;
        }

        public void setSerialNo(int serialNo)
        {
        this.serialNo = serialNo;
        }

        public String getScheduleArrival()
        {
        return scheduleArrival;
        }

        public void setScheduleArrival(String scheduleArrival) 
        {
        this.scheduleArrival = scheduleArrival;
        }

        public String getActualArrival()
        {
        return actualArrival;
        }

        public void setActualArrival(String actualArrival)
        {
        this.actualArrival = actualArrival;
        }

        public String getStation() 
        {

            return station;
        }        

        public void setStation(String station)
        {

            this.station = station;
        }

    public String getStatusOfArrival() 
        {
            return statusOfArrival;
        }

    public void setStatusOfArrival(String statusOfArrival)  
        {
            this.statusOfArrival = statusOfArrival;
        }

    public String getActualArrivalDate() 
        {
            return actualArrivalDate;
        }

    public void setActualArrivalDate(String actualArrivalDate) 
        {
            this.actualArrivalDate = actualArrivalDate;
        }

   public String getScheduleArrivalDate() 

        {

 return scheduleArrivalDate;

}

    public void setScheduleArrivalDate(String scheduleArrivalDate) 
       {
          this.scheduleArrivalDate = scheduleArrivalDate;
       }
}

在这里输入代码

public class TrainStatusParser {

    public static List<TrainStatus> parseFeed(String content){
        try
        {
            JSONObject jsonObject = new JSONObject(content);
            List<TrainStatus> trainList = new ArrayList<>();
            TrainStatus.responseCode = jsonObject.getInt("response_code");
            TrainStatus.position = jsonObject.getString("position");
            TrainStatus.debit=jsonObject.getInt("debit");
            JSONArray route = jsonObject.getJSONArray("route");
             for(int i=0;i<route.length();i++)
             {
                JSONObject routeObject = route.getJSONObject(i);
                TrainStatus trainStatus = new TrainStatus();
                if (jsonObject.has("station"))
                {
                       JSONObject addressObject = jsonObject.getJSONObject("station");
                       trainStatus.name=addressObject.getString("name");
                }
                trainStatus.setSerialNo(routeObject.getInt("no"));
                trainStatus.setScheduleArrival(routeObject.getString("scharr"));
                trainStatus.setActualArrival(routeObject.getString("actarr"));
                trainStatus.setStatusOfArrival(routeObject.getString("status"));
                trainStatus.setHasArrived(routeObject.getBoolean("has_arrived"));
                trainStatus.setHasDeparted(routeObject.getBoolean("has_departed"));
                trainStatus.setLatemin(routeObject.getInt("latemin"));
                trainStatus.setActualArrivalDate(routeObject.getString("actarr_date"));
                trainStatus.setScheduleArrivalDate(routeObject.getString("scharr_date"));
                trainList.add(trainStatus);
            }
            return trainList;

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

除站名

外,所有内容都已正确获取

3 个答案:

答案 0 :(得分:0)

请尝试以下代码。

 for(int i=0;i<route.length();i++) {
            JSONObject routeObject = route.getJSONObject(i);
            TrainStatus trainStatus = new TrainStatus();

            JSONObject jsonObject_station = routeObject.getJSONObject("station");

            String stationname = jsonObject_station.getString("name");
            String code = jsonObject_station.getString("code");
            Log.d("checkvalue", "   " + stationname + "    " + code);

            trainStatus.setSerialNo(routeObject.getInt("no"));
            trainStatus.setScheduleArrival(routeObject.getString("scharr"));
            trainStatus.setActualArrival(routeObject.getString("actarr"));
            trainStatus.setStatusOfArrival(routeObject.getString("status"));
            trainStatus.setHasArrived(routeObject.getBoolean("has_arrived"));
            trainStatus.setHasDeparted(routeObject.getBoolean("has_departed"));
            trainStatus.setLatemin(routeObject.getInt("latemin"));
            trainStatus.setActualArrivalDate(routeObject.getString("actarr_date"));
            trainStatus.setScheduleArrivalDate(routeObject.getString("scharr_date"));
            trainList.add(trainStatus);
        }

答案 1 :(得分:0)

使用来自Google致电GSON

的图书馆

答案 2 :(得分:0)

在您发布的解析每个route的代码中,您有:

            JSONObject routeObject = route.getJSONObject(i);
            TrainStatus trainStatus = new TrainStatus();
            if (jsonObject.has("station"))
            {
                   JSONObject addressObject = jsonObject.getJSONObject("station");
                   trainStatus.name=addressObject.getString("name");
            }

这看起来像是一个错字导致你的问题。当您应该拨打jsonObject.has("station")时,您正在呼叫routeObject.has("station")。同样,在if语句中,您应该将jsonObject.getJSONObject("station")更改为routeObject.getJSONObject("station")