阿里的 Fastjson 开源工具是一款非常好用的工具,用于处理json报文非常方便快捷,在这里我给它点个赞。
fastjson中, JSONObject 类有一个函数 getInnerMap() 函数,可以将 json 字符串转成 java.util.Map 对象。
具体案例分析:
1、先创建一个实体类 User 对象。 注意,在User对象中, createDate 属性使用 fastjson 中的 @JSONField(format = "yyyy-MM-dd HH:mm:ss") 注解来规范它输出时的格式, updateDate 属性没有做任何处理。
package com.fastJson.model2;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import com.alibaba.fastjson.annotation.JSONField;
public class User implements Serializable {
private static final long serialVersionUID = -2938308694021945863L;
private Integer id;
private String name;
private Integer age;
private BigDecimal salary;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createDate;
private Date updateDate;
/**
* 构造函数2
*
* @param id
* @param name
* @param age
*/
public User(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
/**
* 重写toString() 函数
*/
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", createDate="
+ createDate + "]";
}
/**
* 全参构造函数
* @param id
* @param name
* @param age
* @param salary
* @param createDate
* @param updateDate
*/
public User(Integer id, String name, Integer age, BigDecimal salary, Date createDate, Date updateDate) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.createDate = createDate;
this.updateDate = updateDate;
}
/**
* 无参构造函数
*/
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
}
2、在main函数中编写对应的测试用例,将 User 对象转化成 json 字符串,之后将 json 字符串转成 JSONObject 对象,之后将 JSONObject 对象转成 java.util.Map 对象。
package com.fastJson;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastJson.model2.User;
public class TestJsonToMap {
public static void main(String[] args) {
User user1 = new User(new Integer(1001), "张三", new Integer(18));
User user2 = new User(new Integer(1002), "李四", new Integer(20), new BigDecimal(5000), new Date(), new Date());
System.out.println("JSON.toJSONString(user1)为:" + JSON.toJSONString(user1));
System.out.println("JSON.parseObject(JSON.toJSONString(user1))为:" + JSON.parseObject(JSON.toJSONString(user1)));
Map<String, Object> valueMap1 = JSON.parseObject(JSON.toJSONString(user1)).getInnerMap();
System.out.println("valueMap1为:" + valueMap1);
System.out.println("----------------------------------------------------------------");
System.out.println("JSON.toJSONString(user2)为:" + JSON.toJSONString(user2));
System.out.println("JSON.parseObject(JSON.toJSONString(user2))为:" + JSON.parseObject(JSON.toJSONString(user2)));
Map<String, Object> valueMap2 = JSON.parseObject(JSON.toJSONString(user2)).getInnerMap();
System.out.println("valueMap2为:" + valueMap2);
}
}
3、以下是程序运行结果,请仔细分析对比程序运行结果。
JSON.toJSONString(user1)为:{"age":18,"id":1001,"name":"张三"}
JSON.parseObject(JSON.toJSONString(user1))为:{"name":"张三","id":1001,"age":18}
valueMap1为:{name=张三, id=1001, age=18}
----------------------------------------------------------------
JSON.toJSONString(user2)为:{"age":20,"createDate":"2019-12-13 16:03:04","id":1002,"name":"李四","salary":5000,"updateDate":1576224184808}
JSON.parseObject(JSON.toJSONString(user2))为:{"updateDate":1576224184808,"name":"李四","id":1002,"salary":5000,"age":20,"createDate":"2019-12-13 16:03:04"}
valueMap2为:{updateDate=1576224184808, name=李四, id=1002, salary=5000, age=20, createDate=2019-12-13 16:03:04}
由log日志可知,在 JSON.toJSONString(user2) 中,java文件 createDate 属性中添加了 @JSONField(format = "yyyy-MM-dd HH:mm:ss") 注解,所以其 json 字符串中的时间为:"2019-12-13 16:03:04", 字符串格式。 java文件 updateDate 属性没有添加 @JSONField(format = "yyyy-MM-dd HH:mm:ss") 注解,所以其 json 字符串中的时间为:1576224184808,数字格式。
参考文章:
1、 fastjson将json字符串转化成map的五种方法