在項目中使用到了fastjson,故研究了一下。現將本身的幾個測試用例和你們分享一下~java
首先在pom.xml文件中,加入依賴:node
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </dependency>
建立一個實體類:git
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 + "]"; } }
接下來看測試用例:github
一、對象與jsonjson
@Test public void testObject() { XwjUser user = new XwjUser(1, "Hello World", new Date()); user.setNodeName("node"); // 第二個參數爲是否格式化json String jsonStr = JSON.toJSONString(user, true); System.out.println("對象轉爲字符串:" + jsonStr); XwjUser userDes = JSON.parseObject(jsonStr, XwjUser.class); System.out.println("字符串轉爲對象:" + userDes); }
運行結果:ide
對象轉爲字符串:{ "id":1, "message":"Hello World", "nodeName":"node", "sendTime":1525222546733 } 字符串轉爲對象:XwjUser [id=1, message=Hello World, sendTime=Wed May 02 08:55:46 CST 2018, intList=null]
注意事項:測試
一、對象轉json字符串時,對象中的NodeName首字母是大寫,轉出來是小寫。this
二、json轉對象時,實體類中必定要加上默認的無參構造器spa
二、map與json.net
@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("isDel", true); testMap.put("user", new XwjUser(1, "Hello World", new Date())); String jsonStr = JSON.toJSONString(testMap); System.out.println("Map轉爲字符串:" + jsonStr); Map<String, Object> mapDes = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {}); System.out.println("字符串轉map:" + mapDes); }
運行結果:
Map轉爲字符串:{"date":1525223256653,"name":"merry","isDel":true,"user":{"id":1,"message":"Hello World","sendTime":1525223256654},"age":30} 字符串轉map:{date=1525223256653, name=merry, isDel=true, user={"id":1,"message":"Hello World","sendTime":1525223256654}, age=30}
三、list與json
@Test public void testMapList() { List<Map<String, Object>> mapList = new ArrayList<>(); Map<String, Object> map1 = new HashMap<>(); map1.put("name", "merry"); map1.put("age", 30); map1.put("date", new Date()); map1.put("isDel", true); Map<String, Object> map2 = new HashMap<>(); map2.put("name", "jim"); map2.put("age", 28); map2.put("isDel", false); mapList.add(map1); mapList.add(map2); String jsonStr = JSON.toJSONString(mapList); System.out.println("list轉爲字符串:" + jsonStr); // 傳入泛型類型 List<Map> listDes = JSON.parseArray(jsonStr, Map.class); System.out.println("字符串轉爲list:" + listDes); }
運行結果:
list轉爲字符串:[{"date":1525223309870,"name":"merry","isDel":true,"age":30},{"name":"jim","isDel":false,"age":28}] 字符串轉爲list:[{date=1525223309870, name=merry, isDel=true, age=30}, {name=jim, isDel=false, age=28}]
注意:使用JSON.parseArray時,傳入list中的泛型
四、json中日期格式化
@Test public void testDateFormat() { Date now = new Date(); String dateStr = JSON.toJSONString(now); System.out.println("默認日期:" + dateStr); // 按照指定格式格式化日期,格式爲yyyy-MM-dd HH:mm:ss String dateStr2 = JSON.toJSONString(new Date(), SerializerFeature.WriteDateUseDateFormat); System.out.println("格式化日期:" + dateStr2); String dateStr3 = JSON.toJSONStringWithDateFormat(now, "yyyy-MM-dd HH:mm"); System.out.println("格式化日期:" + dateStr3); }
運行結果:
默認日期:1525223374867 格式化日期:"2018-05-02 09:09:34" 格式化日期:"2018-05-02 09:09"
五、json字符串輸出單引號
@Test public void testSingleQuotes() { List<Object> list = new ArrayList<>(); list.add("home"); list.add("hello"); list.add(true); list.add(45.78); String listJson = JSON.toJSONString(list, SerializerFeature.UseSingleQuotes); System.out.println("轉爲單引號字符串:" + listJson); }
運行結果:
轉爲單引號字符串:['home','hello',true,45.78]
六、改變json字符串中默認不輸出值爲null字段
@Test public void testNull() { Map<String, Object> testMap = new HashMap<>(); testMap.put("name", "merry"); testMap.put("age", 30); testMap.put("date", null); // 缺省狀況下,FastJSON不輸入爲值Null的字段 String jsonStr = JSON.toJSONString(testMap); System.out.println("轉爲字符串:" + jsonStr); String jsonStr2 = JSON.toJSONString(testMap, SerializerFeature.WriteMapNullValue); System.out.println("轉爲字符串:" + jsonStr2); }
運行結果:
轉爲字符串:{"name":"merry","age":30} 轉爲字符串:{"date":null,"name":"merry","age":30}
關於SerializerFeature中的常量屬性含義,能夠參考fastjson SerializerFeature詳解
本文示例源碼地址:https://github.com/xuwenjin/xwj_repo/tree/master/xwj-impl