package com.wzc.test.Test; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; class User { private int id; private String name; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return "User [id=" + id + ", name=" + name + ", birthday=" + format.format(birthday) + "]"; } } public class App { public static void main(String[] args) { // 序列化與反序列Bean // 由於Bean中的字段帶有類型,不一樣的類型都能正確處理 // 1. 準備數據 User user = new User(); user.setId(1); user.setName("User"); user.setBirthday(new Date()); // 2. 序列化 String beanStr = JSON.toJSONString(user); System.out.println(beanStr); System.out.println(); // 3. 反序列化 User bean = JSON.parseObject(beanStr, User.class); System.out.println(bean); System.out.println(); // 4. 格式化形式的反序列化 String prettyStr = JSON.toJSONString(user, SerializerFeature.PrettyFormat); System.out.println(prettyStr); System.out.println(); // 5. 使用參數保留類型信息 String classStr = JSON.toJSONString(user, SerializerFeature.WriteClassName, SerializerFeature.PrettyFormat); System.out.println(classStr); System.out.println(); // 序列化與反序列化Map // 1. 準備數據 Map<String, Object> map = new HashMap<>(); map.put("id", 1); map.put("name", "User"); map.put("birthday", new Date()); // 2. 序列化 String mapStr = JSON.toJSONString(map); System.out.println(mapStr); System.out.println(); // 3. 反序列化 // 使用TypeReference提供類型信息 // 因爲Value爲Object類型,能夠看到序列化和反序列化丟失了類型信息 Map<String, Object> m = JSON.parseObject(mapStr, new TypeReference<Map<String, Object>>() { }); System.out.println(m); System.out.println(); // 序列化與反序列化數組 // 1. 準備數據 User user2 = new User(); user2.setId(2); user2.setName("User2"); user2.setBirthday(new Date()); User[] array = new User[] { user, user2 }; // 2. 序列化 String arrayStr = JSON.toJSONString(array); System.out.println(arrayStr); System.out.println(); // 3. 反序列化 List<User> users = JSON.parseArray(arrayStr, User.class); System.out.println(users); System.out.println(); // 4. 使用JSONObject和JSONArray JSONArray jsonArray = JSON.parseArray(arrayStr); for (int i = 0; i < jsonArray.size(); i++) { JSONObject obj = jsonArray.getJSONObject(i); System.out.println(obj); } } }
輸出java
{"birthday":1453037936843,"id":1,"name":"User"} User [id=1, name=User, birthday=2016-01-17] { "birthday":1453037936843, "id":1, "name":"User" } { "@type":"com.wzc.test.Test.User", "birthday":1453037936843, "id":1, "name":"User" } {"birthday":1453037937054,"name":"User","id":1} {birthday=1453037937054, name=User, id=1} [{"birthday":1453037936843,"id":1,"name":"User"},{"birthday":1453037937057,"id":2,"name":"User2"}] [User [id=1, name=User, birthday=2016-01-17], User [id=2, name=User2, birthday=2016-01-17]] {"birthday":1453037936843,"name":"User","id":1} {"birthday":1453037937057,"name":"User2","id":2}