FastjsonTest.java
package demo; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; public class FastjsonTest { public static void main(String[] args) { User user = new User(1, "张三", new Date()); test1(user); test2(user); test3(user); test4(user); test5(user); test6(user); test7(user); test8(); } // 对象转成JSON字符串 private static void test1(User user) { String s = JSON.toJSONString(user); print(1,s); } // 对象转成JSON字符串,格式化日期 private static void test2(User user) { String s = JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat); print(2,s); } // 对象转成JSON字符串,指定日期格式化 private static void test3(User user) { String s = JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd HH:mm:ss.SSS"); print(3,s); } // 对象转成JSON字符串,美化JSON格式 private static void test4(User user) { String s = JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd", SerializerFeature.PrettyFormat); print(4,s); } // 对象转成JSON字符串,默认字段值为NULL不获取,可以加上SerializerFeature.WriteMapNullValue获取 private static void test5(User user) { String s = JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd", SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue); print(5,s); } // JSON字符串转化成对象 private static void test6(User user) { String s = JSON.toJSONString(user); print(6,s); User user2 = JSON.parseObject(s, User.class); print(6,user2.toString()); } //JSON转成Map<String, Object> private static void test7(User user){ String json = JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd"); Map<String, Object> map1 = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){}); print(7, map1.toString()); } //JSON转成List<Map> private static void test8() { User user1 = new User(1, "张三", new Date()); User user2 = new User(2, "李四", new Date()); List<User> list1 = new ArrayList<User>(); list1.add(user1); list1.add(user2); String json = JSON.toJSONString(list1); List<Map> maplist = JSON.parseArray(json, Map.class); print(8, maplist.toString()); } private static void print(int method, String info) { System.out.print("test" + method + "输出:\r\n" + info + "\r\n"); } }
User.java
package demo; import java.util.Date; import java.util.List; public class User { private Integer id; private String name; private Date createDate; private String password; public User(){ } public User(int id, String name, Date createDate) { this.id = id; this.name = name; this.createDate = createDate; } 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 Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "user [id="+id+",name="+name+",createDate="+createDate+",password="+password+"]"; } }
运行结果:
test1输出: {"createDate":1564739625396,"id":1,"name":"张三"} test2输出: {"createDate":"2019-08-02 17:53:45","id":1,"name":"张三"} test3输出: {"createDate":"2019-08-02 17:53:45.396","id":1,"name":"张三"} test4输出: { "createDate":"2019-08-02", "id":1, "name":"张三" } test5输出: { "createDate":"2019-08-02", "id":1, "name":"张三", "password":null } test6输出: {"createDate":1564739625396,"id":1,"name":"张三"} test6输出: user [id=1,name=张三,createDate=Fri Aug 02 17:53:45 CST 2019,password=null] test7输出: {id=1, name=张三, createDate=2019-08-02} test8输出: [{id=1, name=张三, createDate=1564739625490}, {id=2, name=李四, createDate=1564739625490}]