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}]

FastJSON使用例子的更多相关文章

  1. alibaba架包FastJson使用例子

    alibaba的架包FastJson可以对json字符串进行快捷的类型转换.下面是一些各种类型转换的使用例子. 一.下载FastJson的架包,并导入项目中,如下: Maven项目pom.xml配置如 ...

  2. FastJSON 使用

    FastJSON是一个Java语言编写的高性能,功能完善,完全支持http://json.org的标准的JSON库.多了不说了,百度一下一大把. 在此,简单的总结一下自己用过,测试过的方法. 如果使用 ...

  3. 基于STSdb和fastJson的磁盘/内存缓存

    更新 1. 增加了对批量处理的支持,写操作速度提升5倍,读操作提升100倍 2. 增加了对并发的支持 需求 业务系统用的是数据库,数据量大,部分只读或相对稳定业务查询复杂,每次页面加载都要花耗不少时间 ...

  4. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. [转]fastjson常见问题

    转自fastjson wiki说明文档:https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 1. ...

  6. alibaba的FastJson(高性能JSON开发包),fastjson 使用demo

    这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...

  7. 阿里巴巴Json工具-Fastjson讲解

    Fastjson是阿里巴巴公司开源的速度最快的Json和对象转换工具,一个Java语言编写的JSON处理器. 1.遵循http://json.org标准,为其官方网站收录的参考实现之一.2.功能qia ...

  8. Gson 和 FastJson 性能测试

    使用版本: compile 'com.google.code.gson:gson:2.7' compile 'com.alibaba:fastjson:1.2.17' 评测样板为一个People数组, ...

  9. FastJSON 简介及其Map/JSON/String 互转

    在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...

随机推荐

  1. Java并发专题

    ——参考于码农求职小助手公众号 1.并行和并发有什么区别? 1. 并行是指两个或者多个事件在同一时刻发生:而并发是指两个或多个事件在同一时间间隔发生: 2. 并行是在不同实体上的多个事件,并发是在同一 ...

  2. 一文学会JVM配置参数与工具使用

    经过前面的各种分析,我们知道了关于JVM很多的知识,比如版本信息,类加载,堆,方法区,垃圾回收等,但是总觉得心里不踏实,原因是没看到实际的一些东西. 所以这在本文,咱们就好好来聊一聊关于怎么将这些内容 ...

  3. JS(TS)中数组常见的方法(未完待续)

    push():向数组末尾添加一个或多个元素 unshift(): 向数组的开头添加一个或多个元素 pop(): 删除数组最后一个元素 shift(): 删除数组第一个元素 sort(): 给数组排序  ...

  4. 数据处理之以OLEDB方式读取Excel数据丢失的原因及解决方法

    1.引言 在应用程序的设计中,经常需要读取Excel数据或将Excel数据导入转换到其他数据载体中,C#读取Excel的方式有两种,一种是通过OLEDB方式读取,另一种为通过COM组件方式读取.近段时 ...

  5. JS 简介

    JS 简介 JavaScript 是世界上最流行的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备. avaScript 是脚本语言 J ...

  6. redis windows版安装

    首先到 https://github.com/ServiceStack/redis-windows 下载redis 然后将下载的文件解压 然后获取里面的这个压缩包并且解压 这是2.8.2101版本的, ...

  7. oracle12.2RAC之OGG安装配置(二)

    本机到本机的配置: 源端       10.1.83.144:1521/SIBP_GSDY    HNSIB_GSDY目标端   10.1.83.144:1521/SIBP_GS         HN ...

  8. C#后台架构师成长之路-高阶知识体系核心

    了解了这些东西,熟悉了运用基本都是高工级别的了,其他修修补补就行了.... 1.三种预定义特性:attributeUsage.Conditional.obsolete,允许创建自定义特性,派生自Sys ...

  9. bay——巡检RAC命令_版本.txt

    df -lhhostnamecat /etc/hostsifconfig ps -ef | grep tnsps -ef | grep asmps -ef | grep ora_ ls -l /dev ...

  10. 【1期】Java必知必会之一

    面试官:线程池那些事儿 面试官:new 一个对象有哪两个过程?