如何(完全)将 json 反序列化为通用列表?
How to (completely) deserialize json into a generic List?
当使用 ObjectMapper
将 json String
转换为实体时,我可以将其通用化为:
public <E> E getConvertedAs(String body, Class<E> type) throws IOException {
return mapper.readValue(body, type);
}
现在假设我想阅读 collections。我能做到:
List<SomeEntity> someEntityList = asList(mapper.readValue(body, SomeEntity[].class));
List<SomeOtherEntity> someOtherEntityList = asList(mapper.readValue(body, SomeOtherEntity[].class));
我想写一个与上面等效的方法,但是 collections。因为你不能在 java 中使用通用数组,所以像这样的东西是行不通的:
public <E> List<E> getConvertedListAs(String body, Class<E> type) {
return mapper.readValue(body, type[].class);
}
Here 有一个几乎可行的解决方案:
mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});
问题在于它没有反序列化为 E
的列表,而是 LinkedHashMap.Entry
的列表。有什么方法可以更进一步,如下所示?
public <E> List<E> getConvertedListAs(String body, Class<E> type) {
mapper.readValue(body, new TypeReference<List<type>>() {}); // Doesn't compile
}
public static <E> List<E> fromJson(String in_string, Class<E> in_type) throws JsonParseException, JsonMappingException, IOException{
return new ObjectMapper().readValue(in_string, new TypeReference<List<E>>() {});
}
在我的电脑上编译。
请注意,我还没有测试过它。
此方法可以帮助读取 json
对象或集合:
public class JsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
public static <T>T toObject(String json, TypeReference<T> typeRef){
T t = null;
try {
t = mapper.readValue(json, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
}
阅读 json 列出:
List<Device> devices= JsonUtil.toObject(jsonString,
new TypeReference<List<Device>>() {});
读取json对象:
Device device= JsonUtil.toObject(jsonString,
new TypeReference<Device>() {});
当使用 ObjectMapper
将 json String
转换为实体时,我可以将其通用化为:
public <E> E getConvertedAs(String body, Class<E> type) throws IOException {
return mapper.readValue(body, type);
}
现在假设我想阅读 collections。我能做到:
List<SomeEntity> someEntityList = asList(mapper.readValue(body, SomeEntity[].class));
List<SomeOtherEntity> someOtherEntityList = asList(mapper.readValue(body, SomeOtherEntity[].class));
我想写一个与上面等效的方法,但是 collections。因为你不能在 java 中使用通用数组,所以像这样的东西是行不通的:
public <E> List<E> getConvertedListAs(String body, Class<E> type) {
return mapper.readValue(body, type[].class);
}
Here 有一个几乎可行的解决方案:
mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});
问题在于它没有反序列化为 E
的列表,而是 LinkedHashMap.Entry
的列表。有什么方法可以更进一步,如下所示?
public <E> List<E> getConvertedListAs(String body, Class<E> type) {
mapper.readValue(body, new TypeReference<List<type>>() {}); // Doesn't compile
}
public static <E> List<E> fromJson(String in_string, Class<E> in_type) throws JsonParseException, JsonMappingException, IOException{
return new ObjectMapper().readValue(in_string, new TypeReference<List<E>>() {});
}
在我的电脑上编译。 请注意,我还没有测试过它。
此方法可以帮助读取 json
对象或集合:
public class JsonUtil {
private static final ObjectMapper mapper = new ObjectMapper();
public static <T>T toObject(String json, TypeReference<T> typeRef){
T t = null;
try {
t = mapper.readValue(json, typeRef);
} catch (IOException e) {
e.printStackTrace();
}
return t;
}
}
阅读 json 列出:
List<Device> devices= JsonUtil.toObject(jsonString,
new TypeReference<List<Device>>() {});
读取json对象:
Device device= JsonUtil.toObject(jsonString,
new TypeReference<Device>() {});