FastJson的一些使用
前言
最近经常使用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的一些使用的更多相关文章
- fastjson 混淆注意事项
使用fastjson 注意事项,主要表现: 1.加了符号Annotation 的实体类,一使用就会奔溃 2.当有泛型属性时,一使用就奔溃 在调试的时候不会报错,当你要打包签名混淆包的时候,就会出现上述 ...
- Java的Json解析包FastJson使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...
- fastJson使用
fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,由阿里巴巴的工程师开发. 主要特点: 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson ...
- FASTJSON
package com.hanqi.test; import java.util.ArrayList;import java.util.Date;import java.util.List; impo ...
- Android总结之json解析(FastJson Gson 对比)
前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...
- Android原生json和fastjson的简单使用
android原生操作json数据 主要是两个类 JSONObject 操作对象 JONSArray操作json数组 对象转json //创建学生对象 Student student=new ...
- FastJson的简单实用
一.FastJson的理解 在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON ...
- Android JSON、GSON、FastJson的封装与解析
声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...
- java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)
fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...
- FastJson和AsyncHttpCLient
Android的展示数据,除了上章所讲的本地存储外,大部分数据都来自于网络.首先介绍一下Android APP开发常见的网络操作方式.从网络层面上有底层的tcp/ip,也就是我们常见的socket套接 ...
随机推荐
- codevs 1540 银河英雄传说 并查集
1540 银河英雄传说 2002年NOI全国竞赛 时间限制: 1 s 空间限制: 256000 KB 题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星, ...
- Struts2框架学习第一章——Struts2概述
本章要点 — Web应用的发展 — Model 1和Model 2 — MVC思想 — MVC模式的优势 — 常用MVC框架及其特征 — Struts 1的基本结构及其存在的问题 — We ...
- angularjs定时任务的设置与清除
人们似乎常常将AngularJS中 的$timeOut() $interval()函数看做是一个内置的.无须在意的函数.但是,如果你忘记了$timeOut()$interval()的回调函数将会造成 ...
- JavaScript设计模式与开发实践:惰性函数
Web开发中,因为浏览器之间的差异实现差异,一些嗅探工作总是不可避免的,比如我们需要在各个浏览器中能够通用事件绑定函数addEvent //一般写法 //缺点:当他每次被调用的时候都都会执行里面的if ...
- Python打包分发工具setuptools简介(转)
作为Python标准的打包及分发工具,setuptools可以说相当地简单易用.它会随着Python一起安装在你的机器上.你只需写一个简短的setup.py安装文件,就可以将你的Python应用打包. ...
- centos6/7安装 tinyproxy (yum安装)
centos6/7安装tinyproxy(yum安装)2016年06月06日 运维 暂无评论 阅读 790 次centos7安装tinyproxy,centos6安装tinyproxy,centos6 ...
- IOS-H5容器的一些探究:UIWebView和WKWebView的比较和选择
一.Native开发中为什么需要H5容器 Native开发原生应用是手机操作系统厂商(目前主要是苹果的iOS和google的Android)对外界提供的标准化的开发模式,他们对于native开发提供了 ...
- 006-对象—— static关键字 静态属性和方法的使用
<?php /*static()静态属性: */ //静态属性: /*class Model{ private $mysqli; static $config;//数据库连接状态 functio ...
- python虚拟环境的搭建命令mkvirtualenv
windows环境如果同时安装了python3和python2,那么无论在哪个版本安装了virtualenv和virtualenvwrapper-win 通过以下命令设置ptyhon版本路径,即可建立 ...
- certbot以standalone方式新建密钥
下载:wget https://dl.eff.org/certbot-auto 授权:chmod a+x ./certbot-auto 快捷命令 ./certbot-auto certonly --t ...