首页 > IT职业 > IT职场 > json数组转化为list
与xml相比,json小巧但描述能力却不差。其实json就是一串字符串,对于前台传入的json数组,是可以转为list的。本文介绍json数组转化为list的两种方法。
1、依赖ObjectMapper类(使用简洁)
public void testConvert() {
final ObjectMapper mapper = new ObjectMapper();
jsonString=[{"id": "123","name": "李四",}, {"id": "6666","name": "李五",}];
List<DtoBean> listll = mapper.readValue(jsonString, new TypeReference<List<DtoBean>>(){});
}
2、利用JSONArray中的parse方法来解析json数组字符串
List<Map<String,String>> listObjectFir = (List<Map<String,String>>) JSONArray.parse(strArr);
System.out.println;
for(Map<String,String> mapList : listObjectFir){
for (Map.Entry entry : mapList.entrySet()){
System.out.println( entry.getKey() + " " +entry.getValue());
}
}
第一种方法只是针对json数组,是很简单的一种方法。第二种方法主要用于json数组字符串转为list。希望能对你有所帮助哦~
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1600/viewspace-2831373/ ,如需转载,请注明出处,否则将追究法律责任。