前言

最近经常使用json的一些转换,使用的是fastjson,所以就对fastjson进行了一些汇总,记录下来。


正文

主要的api

首先是一些类库的说明:

  SerializeWriter:相当于StringBuffer

  JSONArray:相当于List<Object>

  JSONObject:相当于Map<String, Object>

  JSON反序列化没有真正数组,本质类型都是List<Object>

关于JSON类的一些api:

public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。

细节代码展示

1. json对象与String的互转

JSONObject jsonObject = new JSONObject(); //创建一个json对象
jsonObject.put("hello","hello");
jsonObject.put("world","world");
jsonObject.put("json","json");
String jsonString = jsonObject.toJSONString();  //json转换成String
//打印结果如下:{"world":"world","json":"json","hello":"hello"}
//需要注意的是:put是在前面put进去。
String json = "{\"world\":\"world\",\"json\":\"json\",\"hello\":\"hello\"}"; //这是一个json字符串
JSONObject jsonObject = JSONObject.parseObject(json); //转换成json对象
System.out.println(jsonObject.toJSONString()); //{"world":"world","json":"json","hello":"hello"}
//添加一个新的字段到json对象中并打印
jsonObject.put("content","这是新添加进入的一个字段");
System.out.println(jsonObject.toJSONString()); //{"world":"world","json":"json","hello":"hello","content":"这是新添加进入的一个字段"}

2. json与javaBean的互转

User user = new User();
user.setPassword("111");
user.setUsername("222");
user.setCreateDate(new Date());
String jsonString = JSONObject.toJSONString(user); //将javaBean序列化成json对象(javaBean中的date类型数据不需要格式化)
System.out.println(jsonString); //{"createDate":1532495262966,"password":"111","username":"222"}
//如果时间需要格式化,可以使用下面的
String s = JSONObject.toJSONStringWithDateFormat(user, "yyyy-MM-dd");
System.out.println(s); //{"createDate":"2018-07-25","password":"111","username":"222"}
//目前网上查到的可以识别的时间格式是:yyyy-MM-dd,yyyy-MM-dd HH:mm:ss,yyyy-MM-dd HH:mm:ss,毫秒数字,毫秒数字字符串,new Date(198293238)类型
//将对象字符串转换成javaBean
User parseUser = JSONObject.parseObject(s, User.class);
System.out.println(parseUser.toString()); //User{username='222', password='111', createDate=Wed Jul 25 00:00:00 CST 2018}

3. json与Map的互转

    Map<String,Object> map = new HashMap<>();
map.put("hello","hello");
map.put("world","world");
map.put("json","json");
String jsonString = JSON.toJSONString(map);
String jsonStringPettyFormat = JSON.toJSONString(map,true);
String jsonString1 = JSON.toJSONString(map);
System.out.println(jsonString); //{"world":"world","json":"json","hello":"hello"}
System.out.println(jsonStringPettyFormat);  //带格式的json
{
"world":"world",
"json":"json",
"hello":"hello"
}
System.out.println(jsonString1); //{"world":"world","json":"json","hello":"hello"}
//将json转换成map
Map map1 = JSON.parseObject(jsonString, Map.class); //{world=world, json=json, hello=hello}
Map map2 = JSON.parseObject(jsonStringPettyFormat, Map.class); //{world=world, json=json, hello=hello}
Map map3 = JSON.parseObject(jsonString1, Map.class); //{world=world, json=json, hello=hello}

4. json与List集合的互转

List<User> userList = new ArrayList<>();
userList.add(new User("111","222",new Date()));
userList.add(new User("222","222",new Date()));
String jsonString = JSON.toJSONStringWithDateFormat(userList,"yyyy-MM-dd"); //[{"createDate":"2018-07-25","password":"222","username":"111"},{"createDate":"2018-07-25","password":"222","username":"222"}]
//将json转换成list
List<User> userList1 = JSON.parseArray(jsonString, User.class); //[User{username='111', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}, User{username='222', password='222', createDate=Wed Jul 25 16:49:11 CST 2018}]
List<Map> maps = JSON.parseArray(jsonString, Map.class); //[{password=222, createDate=2018-07-25, username=111}, {password=222, createDate=2018-07-25, username=222}]

特别的:可以将map转换成json然后转换成javaBean

FastJson的一些使用的更多相关文章

  1. fastjson 混淆注意事项

    使用fastjson 注意事项,主要表现: 1.加了符号Annotation 的实体类,一使用就会奔溃 2.当有泛型属性时,一使用就奔溃 在调试的时候不会报错,当你要打包签名混淆包的时候,就会出现上述 ...

  2. Java的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  3. fastJson使用

    fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,由阿里巴巴的工程师开发. 主要特点: 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson ...

  4. FASTJSON

    package com.hanqi.test; import java.util.ArrayList;import java.util.Date;import java.util.List; impo ...

  5. Android总结之json解析(FastJson Gson 对比)

    前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...

  6. Android原生json和fastjson的简单使用

    android原生操作json数据 主要是两个类 JSONObject 操作对象     JONSArray操作json数组 对象转json //创建学生对象 Student student=new ...

  7. FastJson的简单实用

    一.FastJson的理解 在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON ...

  8. Android JSON、GSON、FastJson的封装与解析

    声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...

  9. java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)

     fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...

  10. FastJson和AsyncHttpCLient

    Android的展示数据,除了上章所讲的本地存储外,大部分数据都来自于网络.首先介绍一下Android APP开发常见的网络操作方式.从网络层面上有底层的tcp/ip,也就是我们常见的socket套接 ...

随机推荐

  1. Codeforces Round #341 (Div. 2) C. Mike and Chocolate Thieves 二分

    C. Mike and Chocolate Thieves time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. 【Linux】使用awk批量杀进程

    pkill 杀一个用户的所有进程 pkill -u bingo 批量杀进程 使用ps -ef|grep aaa 查出相关进程 使用grep -v grep 过滤掉grep本身产生的进程 使用awk 打 ...

  3. 深入了解JavaScript中的Symbol的使用方法

    这篇文章主要介绍了深入了解JavaScript中的Symbol的使用方法,本文针对ES6版本的JS进行讲解,需要的朋友可以参考下 Symbol 是什么? Symbols 不是图标,也不是指在代码中可以 ...

  4. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  5. 13.LockSupport工具

    1. LockSupport简介 在之前介绍AQS的底层实现,已经在介绍java中的Lock时,比如ReentrantLock,ReentReadWriteLocks,已经在介绍线程间等待/通知机制使 ...

  6. cf 833 A 数论

    A. The Meaningless Game time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. day35 爬虫简述

    爬虫概要 - pip3 install requests - pip3 install beautifulsoup4 基本爬虫: - Python实现浏览器行为,requests - beautifu ...

  8. log4j 日志详解

    # 以下是rootLogger的配置,子类默认继承,但是子类重写下面配置=rootLogger+自己配置,我晕#输出到控制台 log4j.appender.console=org.apache.log ...

  9. 初次学习AngularJS

    一.指令1.AngularJS 指令是扩展的 HTML 属性,带有前缀 ng-. ng-app 指令初始化一个 AngularJS 应用程序. ng-app 指令定义了 AngularJS 应用程序的 ...

  10. Prism 4 文档 ---第10章 Silverlight和WPF之间共享代码

        本主题帮助你理解来自Prism的多目标和它的优缺点.多目标的代码针对两个不同的平台有大致相同的代码库.这允许同时保持代码尽可能多一样能够产生针对两种不同技术的二进制文件.在这种情况下,本节介绍 ...