从JSON返回空值生成对象列表

时间:2015-12-02 03:31:29

标签: java json rest pojo

您好所有Stackoverflow大师

我正在开发一个应用程序,它从一些其他Web服务中消耗了JSON。

此链接http://pastebin.com/embed_js.php?i=VYESA9MG上的示例JSON(这是由于JSON有点长)

我创建了一个POJO类来满足这个JSON模型,如下所示:

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模型?

1 个答案:

答案 0 :(得分:0)

Hello here是来自github的库,您可以根据您的json数据生成类,并可以相应地获取数据

@Json2Model(modelName = "UserInfo",jsonStr = "Your Json data here";

 String SEARCH_URSER_INFO = "search/user";
  • 最后,您将通过json获得您的回复

    UserInfo userinfo = new Gson()。from(“yourdatagain”,UserInfo.class);