【发布时间】:2015-12-02 03:31:28
【问题描述】:
您好 所有 Stackoverflow 大师,
我正在开发一个应用程序,该应用程序使用来自其他 web 服务的 JSON。
此链接http://pastebin.com/embed_js.php?i=VYESA9MG 上的 JSON 示例(这是由于 JSON 有点长)
我创建了一个满足这种 JSON 模型的 POJO 类,如下所示:
public final class BusSyncAdapterModel {
public final Route routes[];
public BusSyncAdapterModel(Route[] routes){
this.routes = routes;
}
public static final class Route {
public final Route route[];
public final Stop stops[];
public Route(Route[] route, Stop[] stops){
this.route = route;
this.stops = stops;
}
public static final class Routes {
public final End end;
public final Routes route;
public final Start start;
public final Stop stops[];
public final Trip trip;
public Routes(End end, Routes route, Start start, Stop[] stops, Trip trip){
this.end = end;
this.route = route;
this.start = start;
this.stops = stops;
this.trip = trip;
}
public static final class End {
public final String code;
public final long id;
public final double[] location;
public final String name;
public End(String code, long id, double[] location, String name){
this.code = code;
this.id = id;
this.location = location;
this.name = name;
}
}
public static final class RouteList {
public final String code;
public final long id;
public final String name;
public final long type;
public RouteList(String code, long id, String name, long type){
this.code = code;
this.id = id;
this.name = name;
this.type = type;
}
}
public static final class Start {
public final String code;
public final long id;
public final double[] location;
public final String name;
public Start(String code, long id, double[] location, String name){
this.code = code;
this.id = id;
this.location = location;
this.name = name;
}
}
public static final class Stop {
public final String code;
public final long id;
public final long is_wp;
public final String line;
public final double[] location;
public final String name;
public Stop(String code, long id, long is_wp, String line, double[] location, String name){
this.code = code;
this.id = id;
this.is_wp = is_wp;
this.line = line;
this.location = location;
this.name = name;
}
}
public static final class Trip {
public final String headsign;
public final long id;
public Trip(String headsign, long id){
this.headsign = headsign;
this.id = id;
}
}
}
public static final class Stop {
public final Route route;
public final Stop stop;
public final Trip trip;
public Stop(Route route, Stop stop, Trip trip){
this.route = route;
this.stop = stop;
this.trip = trip;
}
public static final class RouteTrip {
public final String code;
public final long id;
public final String name;
public final long type;
public RouteTrip(String code, long id, String name, long type){
this.code = code;
this.id = id;
this.name = name;
this.type = type;
}
}
public static final class StopPoints {
public final String code;
public final long id;
public final double[] location;
public final String name;
public StopPoints(String code, long id, double[] location, String name){
this.code = code;
this.id = id;
this.location = location;
this.name = name;
}
}
public static final class Trip {
public final String headsign;
public final long id;
public Trip(String headsign, long id){
this.headsign = headsign;
this.id = id;
}
}
}
}
}
这个模型在我们使用 GSON 模块的代码中被调用。目前我已经捕捉到 JSON 并且知道 JSON 有两个路由列表。 (请参阅上面的 JSON 示例)但值为 null。
这是我的代码:
private List<BusStopPointsModel> getListBusStopObject(String busRouteCode) {
List<BusStopPointsModel> listOfBusStopObject = new ArrayList<BusStopPointsModel>();
/* load configuration properties */
Prasarana prasarana = new ApiLoader().new Prasarana();
/* do REST web service call */
WebResource webResource = null;
try {
Client client = Client.create();
webResource = client.resource(prasarana.getEndpointURL());
MultivaluedMap<String,String> queryParams = new MultivaluedMapImpl();
queryParams.add("route", busRouteCode);
ClientResponse response = webResource.queryParams(queryParams).get(ClientResponse.class);
if (response.getStatus() != 200) {
logger.info(Constants.SyncAdapter.HTTP_NOTOK_400_BUS + response.getStatus());
throw new RuntimeException(Constants.SyncAdapter.HTTP_NOTOK_400_BUS + response.getStatus());
} else {
logger.info(Constants.SyncAdapter.HTTP_OK_200_BUS);
/* print out the status from server */
String output = response.getEntity(String.class);
JsonObject jsonRoutesObject = new JsonParser().parse(output).getAsJsonObject();
Gson gson = new Gson();
List<BusSyncAdapterModel> listOfBusSyncAdapterModel = new ArrayList<BusSyncAdapterModel>();
Type listType = new TypeToken<List<BusSyncAdapterModel>>() {}.getType();
listOfBusSyncAdapterModel = gson.fromJson(jsonRoutesObject.get("routes"), listType);
System.out.println(listOfBusSyncAdapterModel);
}
} catch (Exception e) {
logger.error(e.getCause());
}
return listOfBusStopObject;
}
有什么想法为什么会下降吗?也许是错误的 POJO 模型?
【问题讨论】:
-
快速浏览一下,我看到的第一个问题是模型类没有默认构造函数。我不使用 Gson,但我认为这是一个问题,因为它不知道如何创建 bean。我知道杰克逊,这是个问题。
-
@peeskillet 我欢迎任何建议。杰克逊是什么意思,这是个问题?
-
我的意思是对于 Jackson(一个不同的 JSON 框架),如果没有默认构造函数(并且没有进一步的配置),它将不知道如何构造对象。
-
@randytan 你确定将输出作为非空值吗?
-
@HBdroid 是的,已知输出列表有两个数组列表。但在列表内部,它是空的。我现在试着把它改成杰克逊。