ObjectMapper
package com.aosiding.user.controller;
import com.aosiding.user.entity.SysUser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.*;
/**
* 使用ObjectMapper操作json结构数据,包括
* bean和json互转、
* list和json互转、
* 数组和json互转、
* map和json互转、
* 流和json互转等操作
* @author AOSDIN
* @Date 2021/9/24
*/
public class ObjectMapperTest {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
SysUser sysUser = new SysUser();
sysUser.setUsername("chen").setPassword("sysUser");
System.out.println("sysUser: "+sysUser);
SysUser sysUser2 = new SysUser();
sysUser2.setUsername("wan").setPassword("sysUser2");
System.out.println("sysUser2: "+sysUser2);
List userList = new ArrayList<>();
userList.add(sysUser);
// 对象转JSON
String string1 = objectMapper.writeValueAsString(sysUser);
System.out.println("对象转JSON sysUser=>json: "+string1);
// JSON转对象
SysUser newUser = objectMapper.readValue(string1, SysUser.class);
System.out.println("JSON转对象 json=>sysUser: "+newUser);
// 集合转JSON
String string2 = objectMapper.writeValueAsString(userList);
System.out.println("集合转JSON List=>json: " + string2);
// JSON转集合
List<SysUser> list = objectMapper.readValue(string2, new TypeReference<List<SysUser>>(){});
System.out.println("JSON转集合 json=>list: " + list);
// 数组转json
SysUser[] userArr = {sysUser,sysUser2};
String string3 = objectMapper.writeValueAsString(userArr);
System.out.println("SysUser[] => json: " + string3);
// json转数组
SysUser[] userJsonToArr = objectMapper.readValue(string3, new TypeReference<SysUser[]>(){});
System.out.println("json => SysUser[]: " + Arrays.toString(userJsonToArr));
HashMap<String, SysUser> map = new HashMap<>();
map.put("1",sysUser);
map.put("2",sysUser2);
// map转json
String string4 = objectMapper.writeValueAsString(map);
System.out.println("map => json: "+string4);
// json转map
HashMap<String, SysUser> JsonToMap = objectMapper.readValue(string4, new TypeReference<HashMap<String, SysUser>>(){});
System.out.println("json => map: " + JsonToMap);
// TODO 流转json
// TODO json转流
}
}
FastJson
package com.aosiding.user.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aosiding.user.entity.SysUser;
import java.util.*;
/**
* @author AOSIDIN
* @Date 2021/9/24
*/
public class FastJsonTest {
public static void main(String[] args) {
SysUser sysUser = new SysUser();
sysUser.setUsername("chen").setPassword("sysUser");
System.out.println("sysUser: "+sysUser);
SysUser sysUser2 = new SysUser();
sysUser2.setUsername("wan").setPassword("sysUser2");
System.out.println("sysUser2: "+sysUser2);
List userList = new ArrayList<>();
userList.add(sysUser);
userList.add(sysUser2);
// 对象转JSON
String string1 = JSON.toJSONString(sysUser);
System.out.println("对象转JSON sysUser=>json: "+string1);
// JSON转对象
SysUser jsonToUser = JSON.parseObject(string1, SysUser.class);
System.out.println("JSON转对象 json=>sysUser: " + jsonToUser);
// 集合转JSON
String string2 = JSON.toJSONString(userList);
System.out.println("集合转JSON List=>json: " + string2);
// JSON转集合
List<SysUser> users = JSON.parseArray(string2, SysUser.class);
System.out.println("JSON转集合 json=>list: " + users);
// 数组转json
SysUser[] userArr = {sysUser,sysUser2};
String string3 = JSON.toJSONString(userArr);
System.out.println("SysUser[] => json: " + string3);
// json转数组
SysUser[] users2 = JSON.parseObject(string3, SysUser[].class);
System.out.println("json => SysUser[]: " + Arrays.toString(users2));
HashMap<String, SysUser> map = new HashMap<>();
map.put("1",sysUser);
map.put("2",sysUser2);
// map转json
String string4 = JSON.toJSONString(map);
System.out.println("map => json: "+string4);
// json转map
Map<String, Object> map2 = JSONObject.parseObject(string4);
System.out.println("json => map: " + map2);
// TODO 流转json
// TODO json转流
}
}