[英]Parsing JSON and Converting in List Of Object
我有从API收到的json响应调用示例响应是这样的
{
"meta": {
"code": "200"
},
"data": [
{
"Id": 44,
"Name": "Malgudi ABC"
},
{
"Id": 45,
"Name": "Malgudi, DEF"
}
]
}
我正在尝试从中创建对象列表,为此我编写的代码是
private static List<TPDetails> getListOfTpDetails(ResponseEntity<?> responseEntity){
ObjectMapper objectMapper = new ObjectMapper();
List<TPDetails> tpDetailsList = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<TPDetails>(){});
return tpDetailsList;
}
TPDetails对象是这样的
public class TPDetails {
int Id;
String Name;
}
我使用的代码导致
java.lang.IllegalArgumentException: Unrecognized field "meta" (class com.sbo.abc.model.TPDetails), not marked as ignorable (2 known properties: "Id", "Name"])
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.sbo.abc.model.TPDetails["meta"])
我想在列表中转换上述JSON响应
List<TPDetails> abc = [
{"Id": 44, "Name": "Malgudi ABC"},
{"Id": 45,"Name": "Malgudi DEF"}
]
任何帮助都是非常可取的。
再创建2个类,例如
public class Temp {
Meta meta;
List<TPDetails> data;
}
public class Meta {
String code;
}
现在将此json转换为Temp类。
Temp temp = objectMapper.convertValue(responseEntity.getBody().getClass(), new TypeReference<Temp>(){});
更新 :
确保responseEntity.getBody()
返回您上面提到的确切的Json字符串。
Temp temp = objectMapper.readValue(responseEntity.getBody(), new TypeReference<Temp>(){});
您的Java类的格式不反映您正在解析的json。 我认为应该是:
class Response {
Meta meta;
List<TPDetails> data;
}
class Meta {
String code;
}
然后,您应该将Response
传递给TypeReference
: new TypeReference<Response>(){}
如果您不关心meta
字段,则可以将@JsonIgnoreProperties
添加到您的响应类,并摆脱Meta
类和字段。
创建/更新以下类,由于没有服务,因此我正在存储JSON文件,但是应该可以解析并从以下模型中读取它。
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"meta",
"data"
})
public class OuterPoJo {
@JsonProperty("meta")
private Meta meta;
@JsonProperty("data")
private List<TPDetails> data = null;
@JsonProperty("meta")
public Meta getMeta() {
return meta;
}
@JsonProperty("meta")
public void setMeta(Meta meta) {
this.meta = meta;
}
@JsonProperty("data")
public List<TPDetails> getData() {
return data;
}
@JsonProperty("data")
public void setData(List<TPDetails> data) {
this.data = data;
}
}
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"code"
})
public class Meta {
@JsonProperty("code")
private String code;
@JsonProperty("code")
public String getCode() {
return code;
}
@JsonProperty("code")
public void setCode(String code) {
this.code = code;
}
}
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 TPDetails {
@JsonProperty("Id")
private Integer id;
@JsonProperty("Name")
private String name;
@JsonProperty("Id")
public Integer getId() {
return id;
}
@JsonProperty("Id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("Name")
public String getName() {
return name;
}
@JsonProperty("Name")
public void setName(String name) {
this.name = name;
}
}
import java.io.File;
public class App {
public static void main(String[] args) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
OuterPoJo myPoJo = objectMapper.readValue(
new File("file.json"),
OuterPoJo.class);
for (TPDetails item : myPoJo.getData()) {
System.out.println(item.getId() + ":" + item.getName());
}
}
}
输出:
44:Malgudi ABC
45:Malgudi, DEF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.