下面代码中 变量 s 是一个 List<LinkedHashMap<String, Object>>类型 但是用 ObjectMapper 的时候
s = mapper.readValue(json, List.class);
只提供了一维上的类型,即 List.class 而不知道二维的类型,即 LinkedHashMap<String, Object> 而后是用
LinkedHashMap<String, Object> link = s.get(i);
得到 link
可是这种 implicit 的转换是怎么个机制? 是 ObjectMapper 的魔力,悄悄的已经转换好了?
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]";
List<LinkedHashMap<String, Object>> s = null;
try {
s = mapper.readValue(json, List.class);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < s.size(); i++) {
LinkedHashMap<String, Object> link = s.get(i);
Set<String> key = link.keySet();
for (Iterator iterator = key.iterator(); iterator.hasNext(); ) {
String string = (String) iterator.next();
System.out.println(string + "==>" + link.get(string));
}
System.out.println("json:" + i + "" + s.get(i).toString());
}
}
1
tsunli 2017-05-03 16:39:26 +08:00
mapper.readValue(json,new TypeReference<List<List<String,String>> (){});
|