个人学习资源免费分享网站:http://xiaocaoshare.com/
public class App {
public static void main(String[] args) {
Map<String, List<Student>> map = new HashMap<String, List<Student>>();
List<Student> list1 = new ArrayList<Student>();
List<Student> list2 = new ArrayList<Student>();
list1.add(new Student("肖1", (byte) 0));
list1.add(new Student("肖2", (byte) 0));
list2.add(new Student("肖3", (byte) 0));
list2.add(new Student("肖4", (byte) 0));
map.put("1", list1);
map.put("2", list2);
String response = JsonMapper.getInstance().toJson(map);
System.out.println(response);
// 解析数据
Map<String, List<Student>> m = JsonMapper.getInstance().fromJson(response,
JsonMapper.getInstance().constructParametricType(HashMap.class, String.class, List.class));
System.out.println(m);
for (Map.Entry<String, List<Student>> entry : m.entrySet()) {
System.out.println("key=" + entry.getKey() + "," + "value=" + entry.getValue());
}
List<Student> list3 = new ArrayList<>();
list3 = map.get("1");
for (Student student : list3) {
System.out.println(student.getName() + student.getSex());
}
}
}
运行结果:
{"1":[{"name":"肖1","sex":0},{"name":"肖2","sex":0}],"2":[{"name":"肖3","sex":0},{"name":"肖4","sex":0}]}
{1=[{name=肖1, sex=0}, {name=肖2, sex=0}], 2=[{name=肖3, sex=0}, {name=肖4, sex=0}]}
key=1,value=[{name=肖1, sex=0}, {name=肖2, sex=0}]
key=2,value=[{name=肖3, sex=0}, {name=肖4, sex=0}]
肖10
肖20