我想只要会fastjson中泛型的使用,Json和Java对象之间的互转没有再难的了。
maven引入fastjson的包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
第一步:将Java对象转换成JSON
List<UserBean> user1List=new ArrayList<UserBean>();
user1List.add(new UserBean("001","王语嫣","温婉如玉"));
user1List.add(new UserBean("002","钟灵","活泼开朗"));
List<UserBean> user2List=new ArrayList<UserBean>();
user2List.add(new UserBean("003","乔峰","侠肝义胆"));
user2List.add(new UserBean("004","段誉","风流倜傥"));
Map<String,List<UserBean>> map=new HashMap<String,List<UserBean>>();
map.put("user1List", user1Multi);
map.put("user2List", user2Multi);
JSON.toJSONString(map);
第二步:将JSON对象转换成Java对象
String json="上面转换后生成的JSON串";
Map<String, List<UserBean>> map = JSON.parseObject(json, new TypeReference<Map<String, List<UserBean>>>() {});