如何将 JSON 数组反序列化为列表?
how to deserialize JSON array to list ?
我正在尝试使用 jackson 消费 JSON。
我想绑定我的 json 的 class 是:-
KeyValueModel.class
public class KeyValueModel {
private String k1;
private String k2;
public String getK1() {
return k1;
}
public void setK1(String k1) {
this.k1 = k1;
}
public String getK2() {
return k2;
}
public void setK2(String k2) {
this.k2 = k2;
}
}
我想将 json 直接映射到我的模型列表,即 KeyValueModel
@Test
public void whenParsingJsonStringIntoList_thenCorrect() throws IOException {
String jsonList = "{
"count": 30,
"data": [
{
"k1": "v1",
"k2": "v2"
},
{
"k1": "no",
"k2": "yes"
}
]
}";
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(List.class, KeyValueModel.class);
List<KeyValueModel> l = mapper.readValue(jsonList, listType);
System.out.println(l.get(1).getK2());
assertNotNull(l);
}
我收到一条错误消息
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (String)"{
"count": 30,
"data": [
{
"k1": "v1",
"k2": "v2"
},
{
"k1": "no",
"k2": "yes"
}
]
}";
如何将数据数组反序列化到列表中?
查看您的 json 数据,它是一个对象,但您正试图将其解析为列表,您在 json 字符串中查找的列表存储在字段 数据,所以试试这样的东西。
JSONObject json = new JSONObject(your_json_string);
JSONArray array = json.getJSONArray("data");
现在您可以通过将 array.toString() 传递给对象映射器
来轻松获得所需的对象列表
谢谢
如果您使用的是 GSON 库,这应该有效:
Type type = new TypeToken<List<KeyValueModel>>(){}.getType();
List<KeyValueModel> l = new Gson().fromJson(jsonList,type);
您需要再创建一个 class,因为您的 json 字符串
中还有一个 属性 计数
public class Test{
private String count;
private KeyValueModel[] kv;
}
然后将您的代码更改为
Test test = mapper.readValue(jsonList, Test.class);
System.out.println(test.getKv()[0].getK1()) will get the expected result
我正在尝试使用 jackson 消费 JSON。
我想绑定我的 json 的 class 是:-
KeyValueModel.class
public class KeyValueModel {
private String k1;
private String k2;
public String getK1() {
return k1;
}
public void setK1(String k1) {
this.k1 = k1;
}
public String getK2() {
return k2;
}
public void setK2(String k2) {
this.k2 = k2;
}
}
我想将 json 直接映射到我的模型列表,即 KeyValueModel
@Test
public void whenParsingJsonStringIntoList_thenCorrect() throws IOException {
String jsonList = "{
"count": 30,
"data": [
{
"k1": "v1",
"k2": "v2"
},
{
"k1": "no",
"k2": "yes"
}
]
}";
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(List.class, KeyValueModel.class);
List<KeyValueModel> l = mapper.readValue(jsonList, listType);
System.out.println(l.get(1).getK2());
assertNotNull(l);
}
我收到一条错误消息
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
at [Source: (String)"{
"count": 30,
"data": [
{
"k1": "v1",
"k2": "v2"
},
{
"k1": "no",
"k2": "yes"
}
]
}";
如何将数据数组反序列化到列表中?
查看您的 json 数据,它是一个对象,但您正试图将其解析为列表,您在 json 字符串中查找的列表存储在字段 数据,所以试试这样的东西。
JSONObject json = new JSONObject(your_json_string);
JSONArray array = json.getJSONArray("data");
现在您可以通过将 array.toString() 传递给对象映射器
来轻松获得所需的对象列表谢谢
如果您使用的是 GSON 库,这应该有效:
Type type = new TypeToken<List<KeyValueModel>>(){}.getType();
List<KeyValueModel> l = new Gson().fromJson(jsonList,type);
您需要再创建一个 class,因为您的 json 字符串
中还有一个 属性 计数public class Test{
private String count;
private KeyValueModel[] kv;
}
然后将您的代码更改为
Test test = mapper.readValue(jsonList, Test.class);
System.out.println(test.getKv()[0].getK1()) will get the expected result