使用Jackson将JSON解析为Java对象
头痛一周后,我来这里寻求你的帮助
我需要使用Jackson反序列化这个JSON输出
{
"data": [
{
"id": "142065955831788",
"name": "Name1",
"link": "http://www.somelink.com",
},
{
"id": "160212467350470",
"name": "Name2",
"link": "http://www.somelink2.com",
}
.
.
.
}
我发誓我什么都试过了,但我就是不能让杰克逊去序列化那个JSON
我错过了什么
编辑:
我创建了这样一个类:
数据。阶级
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data{
@JsonProperty("data")
private String name;
@JsonProperty("data")
private String link;
//Getters + Setters
这是我的映射代码
ObjectMapper mObjectMapper = new ObjectMapper();
ArrayList<Data> mDataList;
mDataList = mObjectMapper.readValue(
url, /* The url returning the JSON */
mObjectMapper.getTypeFactory().constructCollectionType(
ArrayList.class, Data.class
)
);
我收到了这个错误信息:
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
# 1 楼答案
这是代码-
您的JSON也需要更改。它不应该包含数据部分,因为您正在使用它的属性数组
输出-