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套接 ...
随机推荐
- python 这个stdin怎么写
!/usr/bin/env python -- coding: utf-8 -- import json import pprint import sys reload(sys) sys.setdef ...
- C# 同步调用、异步调用、异步回调
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: public dele ...
- Codeforces Round #241 (Div. 2) B. Art Union 基础dp
B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- linux ps 命令参数详解
-a 显示所有终端机下执行的进程,除了阶段作业领导者之外. a 显示现行终端机下的所有进程,包括其他用户的进程. -A 显示所有进程. -c 显示CLS和PRI栏位. c 列出进程时,显示每个进程真正 ...
- Android----- 版本更新和 服务器下载新版本APK并安装
前段时间有朋友问我版本更新的问题,所以来写一篇版本更新和APK下载并安装的博客. 版本更新,几乎在所有的项目中都用的到,一般是这样的流程,当进入APP首页是便会检测版本是否为最新版本,不是则提示你下载 ...
- 设计模式--观察者模式C++实现
观察者模式C++实现 1定义 Observer/Publish/subscribe发布订阅模式 定义对象间一种一对多的依赖关系,使得当一个对象改变状态时,所有依赖他的对象都能获得通知并被自动更新 2类 ...
- jQuery-轮播图(友善滴滚动切换)
线上实例:http://lgy.1zwq.com/slide/ [处理] 这里的图片滚动轮播,做了点小处理:当在第1页状态时,你点击第5页,图片的滚动是一张滑过,而不是从2-3-4-5(这种的多张滚动 ...
- spring boot 中logback多环境配置
spring boot 配置logback spring boot自带了log打印功能,使用的是Commons logging 具体可以参考spring boot log 因此,我们只需要在resou ...
- ( 转)Sqlserver中tinyint, smallint, int, bigint的区别 及 10进制转换16进制的方法
一.类型比较 bigint:从-2^63(-9223372036854775808)到2^63-1(9223372036854775807)的整型数据,存储大小为 8 个字节.一个字节就是8位,那么b ...
- bzoj1711
题解: 原点->食物建一个1 食物->牛见一个1 牛->牛'见一个1 牛'->饮料1 饮料->汇点1 代码: #include<cstdio> #includ ...