你的 JSON 数据模型类应该是这样的
package com.example;
import java.util.ArrayList;
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({
"rows",
"total"
})
public class ReviewsWrapper {
@JsonProperty("rows")
private List<Row> rows = new ArrayList<Row>();
@JsonProperty("total")
private long total;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The rows
*/
@JsonProperty("rows")
public List<Row> getRows() {
return rows;
}
/**
*
* @param rows
* The rows
*/
@JsonProperty("rows")
public void setRows(List<Row> rows) {
this.rows = rows;
}
public ReviewsWrapper withRows(List<Row> rows) {
this.rows = rows;
return this;
}
/**
*
* @return
* The total
*/
@JsonProperty("total")
public long getTotal() {
return total;
}
/**
*
* @param total
* The total
*/
@JsonProperty("total")
public void setTotal(long total) {
this.total = total;
}
public ReviewsWrapper withTotal(long total) {
this.total = total;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public ReviewsWrapper withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
}
-----------------------------------com.example.Row.java-----------------------------------
package com.example;
import java.util.HashMap;
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({
"id",
"name"
})
public class Row {
@JsonProperty("id")
private long id;
@JsonProperty("name")
private String name;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The id
*/
@JsonProperty("id")
public long getId() {
return id;
}
/**
*
* @param id
* The id
*/
@JsonProperty("id")
public void setId(long id) {
this.id = id;
}
public Row withId(long id) {
this.id = id;
return this;
}
/**
*
* @return
* The name
*/
@JsonProperty("name")
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
public Row withName(String name) {
this.name = name;
return this;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public Row withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
}
然后在 Java 类中使用 Jackson 反序列化使用下面的代码
ObjectMapper mapper = new ObjectMapper();
JsonFactory jf = new JsonFactory();
JsonParser jp = jf.createJsonParser("your json data as a String");
ReviewsWrapper reviewWrapper = mapper.readValue(jp,ReviewsWrapper.class);
在此之后您可以从“ReviewsWrapper.class”获得所有回复
这是使用 JsonNode 的示例,试试这个。这是你想要的吗?
这是一个使用节点的示例。
public class App {
public static class Foo {
public int foo;
}
public static void main(String[] args) {
String json = "{\"someArray\":[{\"foo\":5},{\"foo\":6},{\"foo\":7}]}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(json);
node = node.get("someArray");
TypeReference<List<Foo>> typeRef = new TypeReference<List<Foo>>(){};
List<Foo> list = mapper.readValue(node.traverse(), typeRef);
for (Foo f : list) {
System.out.println(f.foo);
} }}