我正在尝试使用Jackson API解析如下的JSON。
{
"name": "John",
"id": 1,
"details": [
{
"item": 2,
"count": 10
},
{
"item": 3,
"count": 5
},
]
}
目标类定义为:
public class Proposal {
private String name;
private Integer id;
private List<Detail> details = new ArrayList<>();
// Setters and getters
}
public class Detail {
private Integer item;
private Integer count;
// Setters and Getters
}
我尝试使用本机数组,但没有成功。我应该使用或创建哪些注释和类才能使转换正常工作?
答案 0 :(得分:0)
有一个非常有用的网站jsonschema2pojo,允许您根据示例JSON生成Java代码。给定您的示例,它将生成:
package com.example;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"id",
"details"
})
public class Proposal {
@JsonProperty("name")
private String name;
@JsonProperty("id")
private Integer id;
@JsonProperty("details")
private List<Detail> details = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("details")
public List<Detail> getDetails() {
return details;
}
@JsonProperty("details")
public void setDetails(List<Detail> details) {
this.details = details;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
答案 1 :(得分:0)
使用此代码
package com;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itextpdf.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
String jsonStr = "{\n" +
" \"name\": \"John\", \n" +
" \"id\": 1, \n" +
" \"details\": [\n" +
" {\n" +
" \"item\": 2, \n" +
" \"count\": 10\n" +
" },\n" +
" {\n" +
" \"item\": 3, \n" +
" \"count\": 5\n" +
" }\n" +
" ]\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
try{
Proposal proposal = mapper.readValue(jsonStr,Proposal.class);
System.out.println(proposal);
}catch(Exception ioe){
ioe.printStackTrace();
}
}
}
class Proposal {
private String name;
private Integer id;
private List<Detail> details;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
// Setters and getters
}
class Detail {
private Integer item;
private Integer count;
public Integer getItem() {
return item;
}
public void setItem(Integer item) {
this.item = item;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
在您的json字符串中,一个逗号是多余的