简体   繁体   English

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

[英]Converting incoming JSON to Java array using Jersey

I have a REST api GET call that takes an array of strings formatted as JSON. 我有一个REST api GET调用,该调用接受一组格式为JSON的字符串。 I'd like to use Jersey to convert that array of strings to something like a string array or List. 我想使用Jersey将字符串数组转换为类似字符串数组或List的内容。 I've reviewed http://jersey.java.net/nonav/documentation/latest/json.html , but it looks like Jersey wants me to create an object that specifies how it should be mapped, which I really don't want to do because it's just a simple array. 我已经审查了http://jersey.java.net/nonav/documentation/latest/json.html ,但是看来Jersey希望我创建一个对象,该对象指定应如何映射它,我真的不想要之所以这样做,是因为它只是一个简单的数组。

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

        }

I know there are lots of libraries for this - but I'd prefer to use Jersey and not introduce any new libraries. 我知道有很多与此相关的库-但是我更喜欢使用Jersey而不引入任何新库。

Create a wrapper object for you data (in this case the Person class) and annotate it with @XMLRootElement 为您的数据创建一个包装对象(在本例中为Person类),并使用@XMLRootElement对其进行注释

Your post method should look like this 您的发布方法应如下所示

@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
}

this is the right way to do this stuff where the data is sent in the request. 这是在请求中发送数据的正确方法。
but if you want to have a QueryParam as the JSON data you can do this 但是,如果您希望将QueryParam作为JSON数据,则可以执行此操作

say your request param looks like this: String persons = "{\\"person\\":[{\\"email\\":\\"asdasd@gmail.com\\",\\"name\\":\\"asdasd\\"},{\\"email\\":\\"Dan@gmail.com\\",\\"name\\":\\"Dan\\"},{\\"email\\":\\"Ion@gmail.com\\",\\"name\\":\\"dsadsa\\"},{\\"email\\":\\"Dan@gmail.com\\",\\"name\\":\\"ertert\\"},{\\"email\\":\\"Ion@gmail.com\\",\\"name\\":\\"Ion\\"}]}"; 说您的请求参数如下所示:字符串person =“ {\\” person \\“:[{\\” email \\“:\\” asdasd@gmail.com \\“,\\” name \\“:\\” asdasd \\“}, {\\“电子邮件\\”:\\“ Dan@gmail.com \\”,\\“名称\\”:\\“ Dan \\”},{\\“电子邮件\\”:\\“ Ion@gmail.com \\”,\\“名称\\“:\\” dsadsa \\“},{\\”电子邮件\\“:\\” Dan@gmail.com \\“,\\”名称\\“:\\” ertert \\“},{\\”电子邮件\\“:\\”离子@ gmail.com \\“,\\”名称\\“:\\”离子\\“}]}”;

you notice that its a JSONObject named "person" that contains a JSONArray of other JSONObjets of type Person with name an email :P you can itterate over them like this: 您会注意到,它的JSONObject名为“ person”,其中包含Person类型的其他JSONObjets的JSONArray,名称为email:P,您可以像这样对它们进行设置:

    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);
    }

sry ry

Just try to add to your Response the array, like 只需尝试将数组添加到您的Response中,例如

return Response.ok(myArray).build();

and see what happen. 看看会发生什么。 If it's just a very simple array it should be parsed without any problem. 如果它只是一个非常简单的数组,则应毫无问题地对其进行解析。

EDIT: 编辑:

If you want to receive it then just accept an array instead of a String. 如果要接收它,则只需接受一个数组而不是一个String。 Try with a List or something like this. 尝试使用列表或类似的方法。

Otherwise you can try to parse it using an ObjectMapper 否则,您可以尝试使用ObjectMapper解析它

mapper.readValue(string, List.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM