1

我有一个如下所示的 JSON 对象:

"TripList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestTrip.xsd",
  "servertime":"11:27",
  "serverdate":"2013-04-02",
  "Trip":[{
    "Leg":{
      "name":"Spårvagn 3",
      "type":"TRAM",
      "id":"9015014500300079",
      "direction":"Kålltorp",
      "fgColor":"#004b85",
      "bgColor":"#ffffff",
      "stroke":"Solid",
      "accessibility":"wheelChair",
      "Origin":{
        "name":"Brunnsparken, Göteborg",
        "type":"ST",
        "id":"9022014001760004",
        "routeIdx":"19",
        "time":"11:27",
        "date":"2013-04-02",
        "track":"D ",
        "rtTime":"11:31",
        "rtDate":"2013-04-02",
        "$":"\n"
        }

获取 Leg 对象内的名称工作正常。但是,如果我想在 Origin 对象中获取时间,我该怎么做呢?

到目前为止,我的代码是这样的:

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(Planner.getPlanner().getJsonDataForTrip(Planner.getPlanner().getStartLocationID(), Planner.getPlanner().getDestinationID()));
    JSONObject topObject = (JSONObject) obj;
    JSONObject locationList = (JSONObject) topObject.get("TripList");
    JSONArray array = (JSONArray) locationList.get("Trip");
    Iterator<JSONObject> iterator = array.iterator();

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();
        jsonObj = (JSONObject) jsonObj.get("Leg");
        String line = (String) jsonObj.get("name");
        Planner.getPlanner().setLines(line);
        System.out.println(jsonObj.get("Origin"));
        Long time = (Long) jsonObj.get("time");
        String track =(String) jsonObj.get("track");

        System.out.println(line);
        System.out.println(time);
        System.out.println(track);

    }

}

And in the console it say like this : 

{"routeIdx":"19","id":"9022014001760004","rtDate":"20130402","time":"15:02","$":"\n","name:"Brunnsparken,哥德堡","track":"D","rtTime":"15:06","date":"2013-04-02","type":"ST"}

斯帕瓦恩 3

无效的

无效的

所以基本上我已经得到了 Spårvang 3 这个名字。但我想抓紧时间。

所以我试图通过使用来摆脱的时间jsonObj.get("time");null有价值的。

有什么问题,我怎样才能从对象“Origin”中获取时间?

4

3 回答 3

2

由于“时间”是“起源”对象的一部分,您需要先提取“起源”对象:

JSONOject origin = (JSONObject) jsonObj.get("Origin");

接着:

String time = origin.getString("time");
于 2013-04-02T13:13:47.493 回答
1

您正在获取“腿”对象的“时间”和“轨道”属性,而不是“原点”对象。它应该是:

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(Planner.getPlanner().getJsonDataForTrip(Planner.getPlanner().getStartLocationID(), Planner.getPlanner().getDestinationID()));
    JSONObject topObject = (JSONObject) obj;
    JSONObject locationList = (JSONObject) topObject.get("TripList");
    JSONArray array = (JSONArray) locationList.get("Trip");
    Iterator<JSONObject> iterator = array.iterator();

    while (iterator.hasNext()) {

        JSONObject jsonObj = iterator.next();
        jsonObj = (JSONObject) jsonObj.get("Leg");
        String line = (String) jsonObj.get("name");
        Planner.getPlanner().setLines(line);
        System.out.println(jsonObj.get("Origin"));

        // Added this line
        jsonObj = (JSONObject) jsonObj.get("Origin");

        String time = (String) jsonObj.get("time");
        String track =(String) jsonObj.get("track");

        System.out.println(line);
        System.out.println(time);
        System.out.println(track);

    }
于 2013-04-02T13:12:17.377 回答
0

执行以下代码

JSONOject origin = (JSONObject) jsonObj.get("Origin");
String time = (String) origin.getString("time");

他们使用 SimpleDateFormat 解析时间字符串并获取日期对象

于 2013-04-02T13:20:29.317 回答