当前位置: 首页 > 知识库问答 >
问题:

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

蓟捷
2023-03-14

嗨,所有Stackoverflow大师,

我正在开发一个应用程序,该应用程序使用来自某个rest webservice的JSON。

此链接上的示例JSONhttp://pastebin.com/embed_js.php?i=VYESA9MG(这是由于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示例)但该值为空。

这是我的代码看起来像:

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个答案

孙佑运
2023-03-14

您好,这里是github的库,您可以根据json数据生成类,并可以相应地获取数据

Json2Model(modelName=“UserInfo”,jsonStr=“此处的Json数据”;

 String SEARCH_URSER_INFO = "search/user";

>

  • 最后,您将通过json获得您的响应

    UserInfo userinfo=new Gson(). from(你的数据再次,UserInfo.class);

  •  类似资料:
    • 我试图返回这个JSON对象中第一个条目的“publisher”和“title”值。 当我运行这段代码时,我可以在开始时返回减去计数部分的对象。 然而,当我试图运行: 我得到一个错误: 我应该在代码中执行哪些操作来打印信息:

    • 以前有人调试过这种问题吗?我可以做什么来解决这个问题并取回一个有效的实例?

    • 我对Spring编程是新手。我正在用Reactor/Webflux项目测试反应式编程。 数据库注册通过POST工作得很好。 我的模型: 我的存储库 我的用户服务

    • 问题内容: 在我的特定情况下,我有两种解决方案。我想找出哪一种更可行。在这种情况下,我还可以通过从服务器端代码返回JSON对象来实现自己的目标。但是,我不知道它是如何完成的,最好的方法是什么。 首先,我不需要完整的aspx页面,因为我只需要从代码返回的响应即可。那么,我是否使用Web服务,处理程序,或者是否有其他特定方法来执行此操作? 这个解决方案可行吗?我是否使用该类构建JSON字符串并将该字符

    • 生成器返回值 PHP7支持通过Generator::getReturn获取生成器方法return的返回值。 PHP5中我们约定使用Generator最后一次yield值作为返回值。 <?php final class AsyncTask { public function begin() { return $this->next(); } //

    • 我有一个方法,它接受一个内部有的对象: MatchData k可以是null,有时k.getWatchListDetail()也可以是null。 我需要检查两种情况。首先,如果它能抛出NPE。 上面的实现可以做到这一点,但我尝试使用或带有链接的流,所以我可以在一行链接中完成。