Jackson JSON+;Java泛型获取LinkedHashMap
我有一个问题类似于stackoverflow的一些问题,但没有一个真正回答我的问题。我使用Jackson的Jackson JSON+;Java泛型获取LinkedHashMap,java,generics,jackson,generic-list,linkedhashmap,Java,Generics,Jackson,Generic List,Linkedhashmap,我有一个问题类似于stackoverflow的一些问题,但没有一个真正回答我的问题。我使用Jackson的ObjectMapper,希望将此JSON字符串解析为用户对象列表: [{ "user" : "Tom", "role" : "READER" }, { "user" : "Agnes", "role" : "MEMBER" }] 我定义了这样一个内部类: public class UserRole { private String user private Stri
ObjectMapper
,希望将此JSON字符串解析为用户对象列表:
[{ "user" : "Tom", "role" : "READER" },
{ "user" : "Agnes", "role" : "MEMBER" }]
我定义了这样一个内部类:
public class UserRole {
private String user
private String role;
public void setUser(String user) {
this.user = user;
}
public void setRole(String role) {
this.role = role;
}
public String getUser() {
return user;
}
public String getRole() {
return role;
}
}
要将JSON字符串解析为UserRoles
列表,我使用泛型:
protected <T> List<T> mapJsonToObjectList(String json) throws Exception {
List<T> list;
try {
list = mapper.readValue(json, new TypeReference<List<T>>() {});
} catch (Exception e) {
throw new Exception("was not able to parse json");
}
return list;
}
protected List mapJsonToObjectList(字符串json)引发异常{
名单;
试一试{
list=mapper.readValue(json,newTypeReference(){});
}捕获(例外e){
抛出新异常(“无法解析json”);
}
退货清单;
}
但是我得到的是一个列表
的LinkedHashMaps
我的代码出了什么问题?根据StaxMan的建议,以下代码可以正常工作,不再使用不推荐使用的static
collectionType()
方法
public class SoApp
{
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
System.out.println("Hello World!");
String s = "[{\"user\":\"TestCity\",\"role\":\"TestCountry\"},{\"user\":\"TestCity\",\"role\":\"TestCountry\"}]";
StringReader sr = new StringReader("{\"user\":\"TestCity\",\"role\":\"TestCountry\"}");
//UserRole user = mapper.readValue(sr, UserRole.class);
mapJsonToObjectList(s,UserRole.class);
}
protected static <T> List<T> mapJsonToObjectList(String json, Class<T> clazz) throws Exception
{
List<T> list;
ObjectMapper mapper = new ObjectMapper();
System.out.println(json);
TypeFactory t = TypeFactory.defaultInstance();
list = mapper.readValue(json, t.constructCollectionType(ArrayList.class,clazz));
System.out.println(list);
System.out.println(list.get(0).getClass());
return list;
}
}
输出
Hello World!
[{"user":"TestCity","role":"TestCountry"},{"user":"TestCity","role":"TestCountry"}]
[UserRole [user=TestCity, role=TestCountry], UserRole [user=TestCity, role=TestCountry]]
class com.test.so.fix.UserRole
它是Java语言,并且臭名昭著的类型擦除:T中有一个类型变量,而不是类型定义;基本上就是说你们要的是清单。Jackson然后使用“自然”对象类型绑定JSON,JSON对象随后成为一个映射(类型为LinkedHashMap,保留顺序)。您通过传递类正确地解决了问题,该类随后定义了实际的type.Correct。一个次要的注意事项是,为了避免弃用问题,可以调用“objectMapper.getTypeFactory()”来使用非静态方法;Jackson 1.8中的这一更改(使用TypeFactory实例)对于支持JVM语言(如Scala)是必要的,Scala有自己的集合/映射类型…@nsfyn55您的答案包括这类问题的两个要点。使用
T
并使用未弃用的方法处理它。投票表决。
Hello World!
[{"user":"TestCity","role":"TestCountry"},{"user":"TestCity","role":"TestCountry"}]
[UserRole [user=TestCity, role=TestCountry], UserRole [user=TestCity, role=TestCountry]]
class com.test.so.fix.UserRole