使用Jersey将传入JSON转换为Java数组

使用Jersey将传入JSON转换为Java数组,java,json,jersey,Java,Json,Jersey,我有一个RESTAPI GET调用,它接受一个格式化为JSON的字符串数组。我想使用Jersey将字符串数组转换为字符串数组或列表。我已经看过了,但Jersey似乎想让我创建一个对象来指定它应该如何映射,我真的不想这样做,因为它只是一个简单的数组 @GET public Response get(@QueryParam("json_items") String requestedItems) throws IOException {

我有一个RESTAPI GET调用,它接受一个格式化为JSON的字符串数组。我想使用Jersey将字符串数组转换为字符串数组或列表。我已经看过了,但Jersey似乎想让我创建一个对象来指定它应该如何映射,我真的不想这样做,因为它只是一个简单的数组

        @GET
        public Response get(@QueryParam("json_items") String requestedItems) throws IOException
        {
            //Would like to convert requestedItems to an array of strings or list

        }

我知道这方面有很多库,但我更喜欢使用Jersey,而不是引入任何新库。

只需尝试将数组添加到您的响应中,如

return Response.ok(myArray).build();
看看会发生什么。 如果它只是一个非常简单的数组,那么应该毫无问题地解析它

编辑:

如果您想接收它,那么只需接受数组而不是字符串。试着列个清单或类似的东西

否则,您可以尝试使用ObjectMapper对其进行解析

mapper.readValue(string, List.class);

只需尝试将数组添加到响应中,如

return Response.ok(myArray).build();
看看会发生什么。 如果它只是一个非常简单的数组,那么应该毫无问题地解析它

编辑:

如果您想接收它,那么只需接受数组而不是字符串。试着列个清单或类似的东西

否则,您可以尝试使用ObjectMapper对其进行解析

mapper.readValue(string, List.class);

为您的数据创建一个包装器对象(在本例中为Person类),并用@XMLRootElement

您的post方法应该如下所示

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void post(List<Person> people) {
    //notice no annotation on the method param
    dao.putAll(people);
    //do what you want with this method
    //also best to return a Response obj and such
}
@POST
@使用(MediaType.APPLICATION_JSON)
公共无效职位(列出人员){
//请注意,方法param上没有注释
道普塔尔(人);
//用这个方法做你想做的
//最好返回一个obj之类的响应
}
这是在请求中发送数据时执行此操作的正确方法。
但是,如果您想将QueryParam作为JSON数据,您可以这样做

假设您的请求参数如下所示: 字符串persons=“{\”person\:[{\”email\“:\”asdasd@gmail.com\“,”姓名\“:\”asdasd\“},{\”电子邮件\“:\”Dan@gmail.com\“,”姓名\“:\”丹\“},{\”电子邮件\“:\”Ion@gmail.com\“,”姓名\“:\”dsadsa \“},{\”电子邮件\“:\”Dan@gmail.com\“,”姓名\“:\”电子邮箱\“:\”Ion@gmail.com\“,\“name\”:\“Ion\”}]}”

您注意到它是一个名为“person”的JSONObject,其中包含一个JSONArray,其中包含一个名为email:p的person类型的其他JSONObject 你可以这样评价他们:

    try {
        JSONObject request = new JSONObject(persons);
        JSONArray arr = request.getJSONArray("person");
        for(int i=0;i<arr.length();i++){
            JSONObject o = arr.getJSONObject(i);
            System.out.println(o.getString("name"));
            System.out.println(o.getString("email"));
        }
    } catch (JSONException ex) {
        Logger.getLogger(JSONTest.class.getName()).log(Level.SEVERE, null, ex);
    }
试试看{
JSONObject请求=新JSONObject(个人);
JSONArray arr=request.getJSONArray(“person”);

对于(int i=0;i为您的数据创建一个包装器对象(在本例中为Person类),并使用@XMLRootElement对其进行注释

您的post方法应该如下所示

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void post(List<Person> people) {
    //notice no annotation on the method param
    dao.putAll(people);
    //do what you want with this method
    //also best to return a Response obj and such
}
@POST
@使用(MediaType.APPLICATION_JSON)
公共无效职位(列出人员){
//请注意,方法param上没有注释
道普塔尔(人);
//用这个方法做你想做的
//最好返回一个obj之类的响应
}
这是在请求中发送数据时执行此操作的正确方法。
但是,如果您想将QueryParam作为JSON数据,您可以这样做

假设您的请求参数如下所示: 字符串persons=“{\”person\:[{\”email\“:\”asdasd@gmail.com\“,”姓名\“:\”asdasd\“},{\”电子邮件\“:\”Dan@gmail.com\“,”姓名\“:\”丹\“},{\”电子邮件\“:\”Ion@gmail.com\“,”姓名\“:\”dsadsa \“},{\”电子邮件\“:\”Dan@gmail.com\“,”姓名\“:\”电子邮箱\“:\”Ion@gmail.com\“,\“name\”:\“Ion\”}]}”

您注意到它是一个名为“person”的JSONObject,其中包含一个JSONArray,其中包含一个名为email:p的person类型的其他JSONObject 你可以这样评价他们:

    try {
        JSONObject request = new JSONObject(persons);
        JSONArray arr = request.getJSONArray("person");
        for(int i=0;i<arr.length();i++){
            JSONObject o = arr.getJSONObject(i);
            System.out.println(o.getString("name"));
            System.out.println(o.getString("email"));
        }
    } catch (JSONException ex) {
        Logger.getLogger(JSONTest.class.getName()).log(Level.SEVERE, null, ex);
    }
试试看{
JSONObject请求=新JSONObject(个人);
JSONArray arr=request.getJSONArray(“person”);

对于(int i=0;iI不想返回它,我想在方法中的某些操作中使用它我不想返回它,我想在方法中的某些操作中使用它你确定需要数组作为
QueryParam
?你确定需要数组作为
QueryParam