Java 从返回空值的JSON生成对象列表
嗨所有Stackoverflow主机Java 从返回空值的JSON生成对象列表,java,json,rest,pojo,Java,Json,Rest,Pojo,嗨所有Stackoverflow主机 我正在开发一个应用程序,该应用程序从一些REST WebService中消耗了JSON。 此链接上的示例JSON(这是由于JSON有点长) 我创建了一个符合此JSON模型的POJO类,如下所示: public final class BusSyncAdapterModel { public final Route routes[]; public BusSyncAdapterModel(Route[] routes){ this.routes =
我正在开发一个应用程序,该应用程序从一些REST WebService中消耗了JSON。 此链接上的示例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;
}
私有列表getListBusStopObject(字符串busRouteCode){
listOfBusStopObject=new ArrayList();
/*加载配置属性*/
Prasarana Prasarana=新的ApiLoader().新的Prasarana();
/*执行RESTWeb服务调用*/
WebResource=null;
试一试{
Client=Client.create();
webResource=client.resource(prasarana.getEndpointURL());
MultivaluedMap queryParams=新的MultivaluedMapImpl();
查询参数。添加(“路线”,总线路线代码);
ClientResponse response=webResource.queryParams(queryParams.get)(ClientResponse.class);
if(response.getStatus()!=200){
logger.info(Constants.SyncAdapter.HTTP_NOTOK_400_BUS+response.getStatus());
抛出新的运行时异常(Constants.SyncAdapter.HTTP_NOTOK_400_BUS+response.getStatus());
}否则{
logger.info(Constants.SyncAdapter.HTTP\u OK\u 200\u总线);
/*从服务器打印出状态*/
字符串输出=response.getEntity(String.class);
JsonObject JSONRouteObject=新建JsonParser().parse(output.getAsJsonObject();
Gson Gson=新的Gson();
List LISTOFBUSSYNCADEPERMODEL=new ArrayList();
类型listType=newTypeToken(){}.getType();
ListOfBusyncAdapterModel=gson.fromJson(jsonRouteObject.get(“路由”),listType);
System.out.println(busyncadaptermodel列表);
}
}捕获(例外e){
logger.error(例如getCause());
}
返回BussTopObject的列表;
}
有什么办法解释为什么会下降吗?可能是错误的POJO模型?Hello是github中的库,您可以根据json数据生成类并相应地获取数据
@Json2Model(modelName=“UserInfo”,jsonStr=“此处的Json数据”;
- 最后,您将通过json获得您的响应 UserInfo UserInfo=new Gson().from(“yourdatagain”,UserInfo.class)
String SEARCH_URSER_INFO = "search/user";