将 json 数组反序列化为 java 对象持有列表
deserialize a json array into a java object holding list
我得到一个 json 数组,其中包含如下所示的对象:
[
{
"id": "1",
"name": "some name",
"url": "some url",
"active": true
}, {
"id": "2",
"name": "some other name",
"url": "some other url",
"active": true
}
]
现在,我希望能够将该数组反序列化为一个 java 对象,其中包含数组中对象的列表。我制作了一个如下所示的自定义解串器:
public class ListSerializer extends JsonDeserializer<List<Provider>>{
private static final long serialVersionUID = 9114152571639338391L;
@Override
public List<Provider> deserialize(JsonParser jsonParser,
DeserializationContext arg1) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
final ObjectCodec objectCodec = jsonParser.getCodec();
final JsonNode listOrObjectNode = objectCodec.readTree(jsonParser);
final List<Provider> result = new ArrayList<Provider>();
for (JsonNode node : listOrObjectNode) {
result.add(objectCodec.treeToValue(node, Provider.class));
}
return result;
}
}
保存列表的 class 如下所示:
public class ProviderList {
@JsonDeserialize(using = ListSerializer.class)
private List<Provider> providerList;
public List<Provider> getProviderList() {
return providerList;
}
public void setProviderList(final List<Provider> providerList) {
this.providerList = providerList;
}
}
我显然做错了什么,因为我收到了这个错误:
Can not deserialize instance of
com.wirelesscar.trailser.v1_0.domain.ProviderList out of START_ARRAY
token at [Source:
[{"id":"1","name":"Posttrack","url":"http:\dev.posttrack.com","active":true},{"id":"2","name":"Trackunit","url":"http:\dev.trackunit.com","active":true}];
line: 1, column: 1]
我怎样才能正确地做到这一点?
您可以使用 TypeReference 包装器直接反序列化为列表。
@Data
public class Provider {
private Long id;
private String name;
private String url;
private boolean active;
}
@Data
public class ProviderList {
List<Provider> providerList;
}
public class JsonTest {
@Test
public void test() {
String json = "[{\n" +
" \"id\": \"1\",\n" +
" \"name\": \"some name\",\n" +
" \"url\": \"some url\",\n" +
" \"active\": true\n" +
" }, {\n" +
" \"id\": \"2\",\n" +
" \"name\": \"some other name\",\n" +
" \"url\": \"some other url\",\n" +
" \"active\": true\n" +
" }\n" +
"]";
ObjectMapper mapper = new ObjectMapper();
try {
List<Provider> providerList = mapper.readValue(json, new TypeReference<List<Provider>>(){});
for (Provider provider : providerList) {
System.out.println(provider);
}
ProviderList list = new ProviderList();
list.setProviderList(providerList);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我得到一个 json 数组,其中包含如下所示的对象:
[
{
"id": "1",
"name": "some name",
"url": "some url",
"active": true
}, {
"id": "2",
"name": "some other name",
"url": "some other url",
"active": true
}
]
现在,我希望能够将该数组反序列化为一个 java 对象,其中包含数组中对象的列表。我制作了一个如下所示的自定义解串器:
public class ListSerializer extends JsonDeserializer<List<Provider>>{
private static final long serialVersionUID = 9114152571639338391L;
@Override
public List<Provider> deserialize(JsonParser jsonParser,
DeserializationContext arg1) throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
final ObjectCodec objectCodec = jsonParser.getCodec();
final JsonNode listOrObjectNode = objectCodec.readTree(jsonParser);
final List<Provider> result = new ArrayList<Provider>();
for (JsonNode node : listOrObjectNode) {
result.add(objectCodec.treeToValue(node, Provider.class));
}
return result;
}
}
保存列表的 class 如下所示:
public class ProviderList {
@JsonDeserialize(using = ListSerializer.class)
private List<Provider> providerList;
public List<Provider> getProviderList() {
return providerList;
}
public void setProviderList(final List<Provider> providerList) {
this.providerList = providerList;
}
}
我显然做错了什么,因为我收到了这个错误:
Can not deserialize instance of com.wirelesscar.trailser.v1_0.domain.ProviderList out of START_ARRAY token at [Source: [{"id":"1","name":"Posttrack","url":"http:\dev.posttrack.com","active":true},{"id":"2","name":"Trackunit","url":"http:\dev.trackunit.com","active":true}]; line: 1, column: 1]
我怎样才能正确地做到这一点?
您可以使用 TypeReference 包装器直接反序列化为列表。
@Data
public class Provider {
private Long id;
private String name;
private String url;
private boolean active;
}
@Data
public class ProviderList {
List<Provider> providerList;
}
public class JsonTest {
@Test
public void test() {
String json = "[{\n" +
" \"id\": \"1\",\n" +
" \"name\": \"some name\",\n" +
" \"url\": \"some url\",\n" +
" \"active\": true\n" +
" }, {\n" +
" \"id\": \"2\",\n" +
" \"name\": \"some other name\",\n" +
" \"url\": \"some other url\",\n" +
" \"active\": true\n" +
" }\n" +
"]";
ObjectMapper mapper = new ObjectMapper();
try {
List<Provider> providerList = mapper.readValue(json, new TypeReference<List<Provider>>(){});
for (Provider provider : providerList) {
System.out.println(provider);
}
ProviderList list = new ProviderList();
list.setProviderList(providerList);
} catch (IOException e) {
e.printStackTrace();
}
}
}