Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从返回空值的JSON生成对象列表_Java_Json_Rest_Pojo - Fatal编程技术网

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

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 =

所有Stackoverflow主机

我正在开发一个应用程序,该应用程序从一些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)


快速看一眼,我看到的第一个问题是模型类没有默认构造函数。我不使用Gson,但我认为这是一个问题,因为it将不知道如何创建bean。我知道Jackson有问题。@peeskillet我非常欢迎任何建议。Jackson有什么问题吗?我是说Jac有问题kson(另一个JSON框架),如果没有默认构造函数(并且没有进一步的配置)它不知道如何构造对象。@randytan你确定输出为非空值吗?@HBdroid是的,输出列表已知有两个arraylist。但在列表中,它是空的。我现在尝试将其更改为Jackson。搜索用户信息是什么意思?该字符串用于什么?@randytan是唯一可用的包名可以在您的buil/generated/apt/debug/search/user中使用
 String SEARCH_URSER_INFO = "search/user";