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套接 ...
随机推荐
- lessc的安装
win+R,cmd打开命令面板,输入cnpm install -g less,如图,然后就可以通过lessc -v查询lessc的版本,出现版本号证明安装成功
- ADO.net笔记
1.DbConnectionConnection对象也称为数据库连接对象,Connection对象的功能是负责对数据源的连接.所有Connection对象的基类都是DbConnection类.Conn ...
- Android自定义圆形ProgressBar
闲来无事做了一个自定义的进度条,大致效果图如下: progressbar.gif 废话不多说,下面直接上代码: 自定义控件代码CircleProgressBar.java: public class ...
- python调用虹软2.0第二版
第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...
- 《Think in Java》(十七)容器深入研究
阿西吧,这一章好长啊,感觉看了快一个月了吧!JDK 自带的容器框架真是很好很强大啊,这一章看得有点蒙蒙的,接下来还得去看看官方文档啊!
- bzoj1014: [JSOI2008]火星人prefix splay+hash+二分
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- windows使用pip安装selenium报错问题
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb9 in position 7: ordinal not in range(128) 这是 ...
- 发送垃圾邮件的僵尸网络——药物(多)、赌博、股票债券等广告+钓鱼邮件、恶意下载链接、勒索软件+推广加密货币、垃圾股票、色情网站(带宏的office文件、pdf等附件)
卡巴斯基实验室<2017年Q2垃圾邮件与网络钓鱼分析报告> 米雪儿 2017-09-07 from:http://www.freebuf.com/articles/network/1465 ...
- p标签多行文字内容实现上下垂直居中兼容ie8
之前实现上下居中一般都是用height和line-height的来设置. 今天在修改样式的时候,p标签的文字内容可能是一行也可能是两行, 所以用height和line-height就没效果. 今天找到 ...
- jquery中prop和attr的区别
jquery中prop和attr的区别 prop: prop(name|properties|key,value|fn) **概述** 获取在匹配的元素集中的第一个元素的属性值. 随着一些内置属性的D ...