类型一:JavaBean
类型二:List
类型三:List
类型四:List<Map<String,Object>>
将上面的四种数据对象转换成json字符串的方法都是一样的
String jsonString = JSON.toJSONString(obj);
fastjson2
Person person = new Person(“1”,“fastjson”,1);
String jsonString = JSON.toJSONString(person);
person =JSON.parseObject(jsonString,Person.class);
Person person1 = new Person(“1”,“fastjson1”,1);
Person person2 = new Person(“2”,“fastjson2”,2);
List persons = new ArrayList();
persons.add(person1);
persons.add(person2);
String jsonString = JSON.toJSONString(persons);
System.out.println(“json字符串:”+jsonString);
List persons2 = JSON.parseArray(jsonString,Person.class);
List list = new ArrayList();
list.add(“fastjson1”);
list.add(“fastjson2”);
list.add(“fastjson3”);
String jsonString = JSON.toJSONString(list);
System.out.println(“json字符串:”+jsonString);
List list2 = JSON.parseObject(jsonString,new TypeReference<List>(){});
将json字符串转化成List<Map<String,Object>>对象
Map<String,Object> map = new HashMap<String,Object>();
map.put(“key1”, “value1”);
map.put(“key2”, “value2”);
Map<String,Object> map2 = new HashMap<String,Object>();
map2.put(“key1”, 1);
map2.put(“key2”, 2);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
list.add(map);
list.add(map2);
String jsonString = JSON.toJSONString(list);
System.out.println(“json字符串:”+jsonString);
List<Map<String,Object>> list2 = JSON.parseObject(jsonString,new TypeReference<List<Map<String,Object>>>(){});