【问题标题】:retrofit call on object that contains another object represented by an array对包含由数组表示的另一个对象的对象的改造调用
【发布时间】:2018-08-01 03:41:08
【问题描述】:

我有 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 模型中?我对(据我了解)它是一个包含数组的对象感到困惑,我不确定在这种情况下如何表示它。以及如何在改造电话中实际从中获取数据?我需要为每条路线获取每个目的地。

【问题讨论】:

  • 这是ListSchedule 实体。您只需要创建一个具有正确属性的Schedule 类并将private List<Schedule> schedules' 添加到您的Route 对象

标签: java json object retrofit


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    相关资源
    最近更新 更多