gpt4 book ai didi

java - 想要使用 arraylist 将 json 字符串反序列化为 java 对象

转载 作者:行者123 更新时间:2023-12-01 22:09:46 25 4
gpt4 key购买 nike

java对象:MyObject有AnotherObject1的列表,AnotherObject1也有AnotherObject2的列表

class MyObject{

private String status;

private String message;

private List<AnotherObject1> data;
public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public List<AnotherObject1> getData() {
return data;
}

public void setData(List<AnotherObject1> data) {
this.data = data;
}
}

Class AnotherObject1{
private Integer group_id;

private List<AnotherObject2> anotherList;
public Integer getGroup_id() {
return group_id;
}

public void setGroup_id(Integer group_id) {
this.group_id = group_id;
}

public List<AnotherObject2> getAnotherList() {
return smsList;
}

public void setAnotherList(List<AnotherObject2> anotherList) {
this.anotherList = anotherList;
}

}

class AnotherObject2{
private String customid;

private String customid1 ;

private Long mobile;

private String status;

private String country;

public String getCustomid() {
return customid;
}

public void setCustomid(String customid) {
this.customid = customid;
}

public String getCustomid1() {
return customid1;
}

public void setCustomid1(String customid1) {
this.customid1 = customid1;
}

public Long getMobile() {
return mobile;
}

public void setMobile(Long mobile) {
this.mobile = mobile;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}


}

JSON 字符串:这是我的 json 字符串,我想通过它使用对象映射器创建一个 java 对象

String response="{\"status\":\"OK\",\"data\":{\"group_id\":39545922,\"0\":{\"id\":\"39545922-1\",\"customid\":\"\",\"customid1\":\"\",\"customid2\":\"\",\"mobile\":\"910123456789\",\"status\":\"XYZ\",\"country\":\"IN\"}},\"message\":\"WE R Happy.\"}"

ObjectMapper 代码//将字符串转换为响应对象

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.readValue(responseBody, MyObject.class);

异常:这是异常

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: {"status":"OK","data":{"group_id":39545922,"0":{"id":"39545922-1","customid":"","customid1":"","customid2":"","mobile":"910123456789","status":"GOOD","country":"IN"}},"message":"We R happy."}; line: 1, column: 15] (through reference chain: MyObject["data"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:850)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:292)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:256)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
at abc.disp(RestClientImpl.java:210)
at abc.disp(RestClientImpl.java:105)
at Application.<init>(Application.java:42)
at Application.main(Application.java:45)

请指导我如何实现这一目标。

最佳答案

您的代码本身不可编译...

private List<AnotherObject1> data; // Your class member is list of AnotherObject1

下面它用作 getter 和 setter 中的 SMSDTO 列表

public List<SMSDTO> getData() {
return data;
}

关于java - 想要使用 arraylist 将 json 字符串反序列化为 java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com