问题
I have a JSON string with multiple key-value pairs and few single objects as under.
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.
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"
}
]
}
My resource class:
@POST
@Path("/somePath")
@Consumes(MediaType.APPLICATION_JSON)
public Response generateDetails(MyObject myObject) {
myObject.getName();
System.out.println("Name " + myObject.getName());
}
回答1:
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
}
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);
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 parsing will the automatically handled by the JacksonJsonProvider class, which implements MessageBodyReader.
Jackson dependencies
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>
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>
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.
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.
Update based in your requirement change
Change the RequestContent class to be like:
public class RequestContent {
@JsonProperty("Name")
private String name;
@JsonProperty("MyMap")
private List<List<KeyValuePair>> map;
// Getters and setters ommited
}
Then parse the JSON with one of the approaches shown above.
回答2:
1. Create a Pojo class having same keys like your json string contains:
eg: name , arraylist for your array
2. Read the above json string and then convert to your pojo.
eg:
ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'gokul'}";
Staff obj = mapper.readValue(jsonInString, Staff.class);
来源:https://stackoverflow.com/questions/38779319/parsing-json-in-jersey