阿里的 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的五種方法