对包含另一个以数组表示的对象的对象进行改造调用

时间:2018-07-31 19:25:46

标签: java json object retrofit

我有json数据:

[
  {
    "RouteNo": "004",
    "RouteName": "POWELL/DOWNTOWN/UBC",
    "Direction": "WEST",
    "Schedules": [
       {
        "Destination": "UBC",
        "ExpectedCountdown": -4,
       },
       {
        "Destination": "Downtown",
        "ExpectedCountdown": -6,
       }
    ]
  },
  {
    "RouteNo": "006",
    "RouteName": "Grange str",
    "Direction": "South",
    "Schedules": [
       {
        "Destination": "Victoria",
        "ExpectedCountdown": -9,
       },
       {
        "Destination": "College station",
        "ExpectedCountdown": -15,
       }
    ]
   }
]

我有我的模型Route.java:

public class Route {
    public String getRouteNo() {
        return RouteNo;
    }

    public void setRouteNo(String routeNo) {
        RouteNo = routeNo;
    }

    public String getRouteName() {
        return RouteName;
    }

    public void setRouteName(String routeName) {
        RouteName = routeName;
    }


    public Route(String routeNo, String routeName) {
        RouteNo = routeNo;
        RouteName = routeName;

    }

    private String RouteNo;
    private String RouteName;


}

我的问题是如何将可以在json数据中看到的Schedules对象插入Route模型?就我所知,我对它是一个包含数组的对象感到困惑,我不确定在这种情况下如何表示它。以及实际上如何在改造调用中从中获取数据?我需要获取每个路线的每个目的地。

1 个答案:

答案 0 :(得分:0)

Route更新为:

public class Route {
    public String getRouteNo() {
        return RouteNo;
    }

    public void setRouteNo(String routeNo) {
        RouteNo = routeNo;
    }

    public String getRouteName() {
        return RouteName;
    }

    public void setRouteName(String routeName) {
        RouteName = routeName;
    }


    public Route(String routeNo, String routeName) {
        RouteNo = routeNo;
        RouteName = routeName;

    }

    private String RouteNo;
    private String RouteName;

    private List<Schedule> schedules;  // add mutators with @JsonProperty annotation

}

然后按照上述JSON的描述创建Schedule