需要的依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
常用方法
public String writeValueAsString(Object value) 将一个对象转换为json格式的字符串
public <T> T readValue(String content, Class<T> valueType) json格式字符串转换为指定格式的对象
public class XwjUser implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String message;
private Date sendTime;
// 这里手写字母大写,只是为了测试使用,是不符合java规范的
private String NodeName;
private List<Integer> intList;
public XwjUser() {
super();
}
public XwjUser(int id, String message, Date sendTime) {
super();
this.id = id;
this.message = message;
this.sendTime = sendTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Date getSendTime() {
return sendTime;
}
public void setSendTime(Date sendTime) {
this.sendTime = sendTime;
}
public String getNodeName() {
return NodeName;
}
public void setNodeName(String nodeName) {
NodeName = nodeName;
}
public List<Integer> getIntList() {
return intList;
}
public void setIntList(List<Integer> intList) {
this.intList = intList;
}
@Override
public String toString() {
return "XwjUser [id=" + id + ", message=" + message + ", sendTime=" + sendTime + ", intList=" + intList + "]";
}
}
创建ObjectMapper
public static ObjectMapper mapper = new ObjectMapper();
static {
// 转换为格式化的json
mapper.enable(SerializationFeature.INDENT_OUTPUT);
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
对象与JSON
@Test
public void testObj() throws JsonGenerationException, JsonMappingException, IOException {
XwjUser user = new XwjUser(1, "Hello World", new Date());
mapper.writeValue(new File("D:/test.txt"), user); // 写到文件中
// mapper.writeValue(System.out, user); //写到控制台
String jsonStr = mapper.writeValueAsString(user);
System.out.println("对象转为字符串:" + jsonStr);
byte[] byteArr = mapper.writeValueAsBytes(user);
System.out.println("对象转为byte数组:" + byteArr);
XwjUser userDe = mapper.readValue(jsonStr, XwjUser.class);
System.out.println("json字符串转为对象:" + userDe);
XwjUser useDe2 = mapper.readValue(byteArr, XwjUser.class);
System.out.println("byte数组转为对象:" + useDe2);
}
对象转为字符串:{
"id" : 1,
"message" : "Hello World",
"sendTime" : 1525163446305,
"intList" : null,
"nodeName" : null
}
对象转为byte数组:[B@3327bd23
json字符串转为对象:XwjUser [id=1, message=Hello World, sendTime=Tue May 01 16:30:46 CST 2018, intList=null]
byte数组转为对象:XwjUser [id=1, message=Hello World, sendTime=Tue May 01 16:30:46 CST 2018, intList=null]
List与JSON
@Test
public void testList() throws JsonGenerationException, JsonMappingException, IOException {
List<XwjUser> userList = new ArrayList<>();
userList.add(new XwjUser(1, "aaa", new Date()));
userList.add(new XwjUser(2, "bbb", new Date()));
userList.add(new XwjUser(3, "ccc", new Date()));
userList.add(new XwjUser(4, "ddd", new Date()));
String jsonStr = mapper.writeValueAsString(userList);
System.out.println("集合转为字符串:" + jsonStr);
List<XwjUser> userListDes = mapper.readValue(jsonStr, List.class);
System.out.println("字符串转集合:" + userListDes);
}
字符串:[ {
"id" : 1,
"message" : "aaa",
"sendTime" : 1525164096846,
"intList" : null,
"nodeName" : null
}, {
"id" : 2,
"message" : "bbb",
"sendTime" : 1525164096846,
"intList" : null,
"nodeName" : null
}, {
"id" : 3,
"message" : "ccc",
"sendTime" : 1525164096846,
"intList" : null,
"nodeName" : null
}, {
"id" : 4,
"message" : "ddd",
"sendTime" : 1525164096846,
"intList" : null,
"nodeName" : null
} ]
字符串转集合:[{id=1, message=aaa, sendTime=1525164096846, intList=null, nodeName=null}, {id=2, message=bbb, sendTime=1525164096846, intList=null, nodeName=null}, {id=3, message=ccc, sendTime=1525164096846, intList=null, nodeName=null}, {id=4, message=ddd, sendTime=1525164096846, intList=null, nodeName=null}]
map与JSON
@Test
public void testMap() {
Map<String, Object> testMap = new HashMap<>();
testMap.put("name", "merry");
testMap.put("age", 30);
testMap.put("date", new Date());
testMap.put("user", new XwjUser(1, "Hello World", new Date()));
try {
String jsonStr = mapper.writeValueAsString(testMap);
System.out.println("Map转为字符串:" + jsonStr);
try {
Map<String, Object> testMapDes = mapper.readValue(jsonStr, Map.class);
System.out.println("字符串转Map:" + testMapDes);
} catch (IOException e) {
e.printStackTrace();
}
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
Map转为字符串:{
"date" : 1525164199804,
"name" : "merry",
"user" : {
"id" : 1,
"message" : "Hello World",
"sendTime" : 1525164199805,
"intList" : null,
"nodeName" : null
},
"age" : 30
}
字符串转Map:{date=1525164199804, name=merry, user={id=1, message=Hello World, sendTime=1525164199805, intList=null, nodeName=null}, age=30}
日期时间与JSON
@Test
public void testOther() throws IOException {
// 修改时间格式
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
XwjUser user = new XwjUser(1, "Hello World", new Date());
user.setIntList(Arrays.asList(1, 2, 3));
String jsonStr = mapper.writeValueAsString(user);
System.out.println("对象转为字符串:" + jsonStr);
}
对象转为字符串:{
"id" : 1,
"message" : "Hello World",
"sendTime" : "2018-05-01 16:44:06",
"intList" : [ 1, 2, 3 ],
"nodeName" : null
}