static class TempClass{
private String name;
private int age;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
}
public static void main(String[] args) {
Map> map = new HashMap>();
Random random = new Random();
for(int i = 1 ; i < 3 ;i++){
List list = new ArrayList();
for(int j = 1 ; j < 3 ;j++) {
TempClass tempClass = new TempClass();
int r = random.nextInt(500);
tempClass.name = r +"name";
tempClass.age = r ;
list.add(tempClass);
}
map.put(UUID.randomUUID().toString(),list);
}
//===============对象转字符串=====================================
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
//===============字符串转对象===========================
Map> mapObj = (HashMap>)JSON.parseObject(jsonString, HashMap.class);
Map> mapList = (HashMap>)JSON.parseObject(jsonString, HashMap.class);
for(Map.Entry> entry : mapObj.entrySet()){
// 此时entry.getValue()并不具备类型信息,需要再做深层的解析
List list = (List)JSONArray.parseArray(entry.getValue().toString(), TempClass.class);
mapList.put(entry.getKey(),list);
}
}