在Jersey中解析JSON [英] Parsing JSON in Jersey

查看:143
本文介绍了在Jersey中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON字符串,其中包含多个键-值对和下面的几个单个对象.

I have a JSON string with multiple key-value pairs and few single objects as under.

我正在使用Jersey编写一个资源类,该资源类将读取此JSON并将其转换为Java对象,但是我无法这样做.

I am using Jersey to write a resource class that will read this JSON and convert it to a Java object but I am unable to do so.

有人可以帮助我编写相应的Java对象以及如何解析JSON来填充此Java对象吗?

Can anyone help me to write the corresponsing Java object and how to parse the JSON to populate this Java object?

{
  "Name": "TestName",
  "MyMap": [
    {
      "key": "Color",
      "value": "red"
    },
    {
      "key": "distance",
      "value": "long"
    },
    {
      "key": "Size",
      "value": "large"
    }
  ]
}

我的资源分类:

@POST
@Path("/somePath")
@Consumes(MediaType.APPLICATION_JSON)
public Response generateDetails(MyObject myObject) {
    myObject.getName();
    System.out.println("Name " + myObject.getName());
}

推荐答案

设计POJO以与Jackson一起使用

如果您愿意将Jackson用作JSON解析器,则可以进行以下操作:

Designing your POJO to work with Jackson

If you are open to use Jackson as a JSON parser, you can have the following:

public class RequestContent {

    @JsonProperty("Name")
    private String name;

    @JsonProperty("MyMap")
    private List<KeyValuePair> map;

    // Getters and setters ommited
}

public class KeyValuePair {

    private String key;

    private String value;

    // Getters and setters ommited
}

用Jackson解析JSON

使用Jersey和Jackson,您无需使用ObjectMapper解析JSON字符串,如下所示:

Parsing the JSON with Jackson

With Jersey and Jackson, you won't need to parse your JSON string with ObjectMapper, as shown below:

String json = "{\"Name\":\"TestName\",\"MyMap\":[{\"key\":\"Color\",\"value\":\"red\"},{\"key\":\"distance\",\"value\":\"long\"},{\"key\":\"Size\",\"value\":\"large\"}]}";";

ObjectMapper mapper = new ObjectMapper();
RequestContent requestContent = mapper.readValue(json, RequestContent.class);

只需确保您的资源方法如下所示,并让Jackson的

Just ensure your resource method look as following and let Jackson's MessageBodyReader do the job for you:

@POST 
@Path("/somePath") 
@Consumes(MediaType.APPLICATION_JSON) 
public Response generateDetails(RequestContent requestContent) {
    ...
}

JSON解析将由

JSON parsing will the automatically handled by the JacksonJsonProvider class, which implements MessageBodyReader.

要将Jackson 2.x用作JSON提供程序,您需要添加

To use Jackson 2.x as your JSON provider you need to add jersey-media-json-jackson module to your pom.xml file:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.23.1</version>
</dependency>

要使用Jackson 1.x,它的外观如下:

To use Jackson 1.x it'll look like:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson1</artifactId>
    <version>2.23.1</version>
</dependency>

如果您不使用Maven,请确保具有所有必需的依赖项(请参见

If you're not using Maven make sure to have all needed dependencies (see jersey-media-json-jackson or jersey-media-json-jackson1) on the classpath.

要使用Jackson作为您的JSON提供程序,您需要注册

In order to use Jackson as your JSON provider you need to register JacksonFeature (or Jackson1Feature for Jackson 1.x) in your Application/ResourceConfig sub-class.

有关更多详细信息,请参阅泽西文档关于对常见媒体类型的支持表示形式.

For more details, check the Jersey documentation about support for common media type representations.

RequestContent类更改为:

public class RequestContent {

    @JsonProperty("Name")
    private String name;

    @JsonProperty("MyMap")
    private List<List<KeyValuePair>> map;

    // Getters and setters ommited
}

然后使用上面显示的方法之一解析JSON.

Then parse the JSON with one of the approaches shown above.

这篇关于在Jersey中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆