fastJson转换对象与字符串

前端之家收集整理的这篇文章主要介绍了fastJson转换对象与字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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<String,List<Test.TempClass>> map = new HashMap<String,List<Test.TempClass>>();Random random = new Random();for(int i = 1 ; i < 3 ;i++){List<Test.TempClass> list = new ArrayList<Test.TempClass>();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<String,List<?>> mapObj = (HashMap<String,List<?>>)JSON.parSEObject(jsonString,HashMap.class);Map<String,List<TempClass>> mapList = (HashMap<String,List<TempClass>>)JSON.parSEObject(jsonString,HashMap.class);for(Map.Entry<String,List<?>> entry : mapObj.entrySet()){// 此时entry.getValue()并不具备类型信息,需要再做深层的解析List<TempClass> list = (List<TempClass>)JSONArray.parseArray(entry.getValue().toString(),TempClass.class);mapList.put(entry.getKey(),list);}}

原文链接:/json/288570.html

猜你在找的Json相关文章