强大的json工具:fastJson
fastJson
1、遵循http://json.org标准,为其官方网站收录的参考实现之一。
2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。
4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home
SVN:http://code.alibabatech.com/svn/fastjson/trunk/
WIKI:http://code.alibabatech.com/wiki/display/FastJSON/Home
Issue Tracking:http://code.alibabatech.com/jira/browse/FASTJSON
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。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.util.*;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 11-11-22 上午9:15
*/
public class Foo {
private String vString = "vStringhehhehe";
private char vchar = 'x';
private byte vbyte = 64;
private short vshort = 128;
private int vint = 65535;
private long vlong = 9999999L;
private float vfloat = 12.1f;
private double vdouble = 22.203d;
private boolean vboolean = false;
// private Date vdate = new Date();
private Date dddd = new Date();
private Date vDate = new Date();
private Date v_Date = new Date();
private Object vnull = null;
private String[] avString = {"aaa", "bbb", "ccc"};
private int[] avint = {1, 2, 3, 4};
private boolean[] avboolean = {true, false, true, true};
private List<String> listString = new ArrayList<String>();
private Map<String, String> map = new HashMap<String, String>();
private Bar bar = new Bar();
private Bar[] avBar = {new Bar(), new Bar()};
private List<Bar> listBar = new ArrayList<Bar>();
{
listString.add("listString1");
listString.add("listString2");
listString.add("listString3");
map.put("x", "s11111x");
map.put("y", "s22222y");
map.put("z", "s33333z");
listBar.add(new Bar());
listBar.add(new Bar());
listBar.add(new Bar());
}
public String getvString() {
return vString;
}
public void setvString(String vString) {
this.vString = vString;
}
public char getVchar() {
return vchar;
}
public void setVchar(char vchar) {
this.vchar = vchar;
}
public byte getVbyte() {
return vbyte;
}
public void setVbyte(byte vbyte) {
this.vbyte = vbyte;
}
public short getVshort() {
return vshort;
}
public void setVshort(short vshort) {
this.vshort = vshort;
}
public int getVint() {
return vint;
}
public void setVint(int vint) {
this.vint = vint;
}
public long getVlong() {
return vlong;
}
public void setVlong(long vlong) {
this.vlong = vlong;
}
public float getVfloat() {
return vfloat;
}
public void setVfloat(float vfloat) {
this.vfloat = vfloat;
}
public double getVdouble() {
return vdouble;
}
public void setVdouble(double vdouble) {
this.vdouble = vdouble;
}
public boolean isVboolean() {
return vboolean;
}
public void setVboolean(boolean vboolean) {
this.vboolean = vboolean;
}
public Date getDddd() {
return dddd;
}
public void setDddd(Date dddd) {
this.dddd = dddd;
}
public Date getV_Date() {
return v_Date;
}
public void setV_Date(Date v_Date) {
this.v_Date = v_Date;
}
public Date getvDate() {
return vDate;
}
public void setvDate(Date vDate) {
this.vDate = vDate;
}
public Object getVnull() {
return vnull;
}
public void setVnull(Object vnull) {
this.vnull = vnull;
}
public String[] getAvString() {
return avString;
}
public void setAvString(String[] avString) {
this.avString = avString;
}
public int[] getAvint() {
return avint;
}
public void setAvint(int[] avint) {
this.avint = avint;
}
public boolean[] getAvboolean() {
return avboolean;
}
public void setAvboolean(boolean[] avboolean) {
this.avboolean = avboolean;
}
public List<String> getListString() {
return listString;
}
public void setListString(List<String> listString) {
this.listString = listString;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Bar getBar() {
return bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
public Bar[] getAvBar() {
return avBar;
}
public void setAvBar(Bar[] avBar) {
this.avBar = avBar;
}
public List<Bar> getListBar() {
return listBar;
}
public void setListBar(List<Bar> listBar) {
this.listBar = listBar;
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.util.Date;
import java.util.Random;
/**
* Created by IntelliJ IDEA.
*
* @author leizhimin 11-11-22 上午9:15
*/
public class Bar {
public static SerializeConfig mapping = new SerializeConfig();
private String barName;
private int barAge;
private Date barDate = new Date();
static {
mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd"));
}
{
Random r = new Random();
barName = "sss_"+String.valueOf(r.nextFloat());
barAge = r.nextInt();
}
public static void main(String[] args) {
Object obj = JSON.toJSON(new Bar());
String x1 = JSON.toJSONString(new Bar(), true);
System.out.println(x1);
String x2 = JSON.toJSONString(new Bar(), mapping);
System.out.println(x2);
}
public String getBarName() {
return barName;
}
public void setBarName(String barName) {
this.barName = barName;
}
public int getBarAge() {
return barAge;
}
public void setBarAge(int barAge) {
this.barAge = barAge;
}
public Date getBarDate() {
return barDate;
}
public void setBarDate(Date barDate) {
this.barDate = barDate;
}
@Override
public String toString() {
return "Bar{" +
"barName='" + barName + '\'' +
", barAge=" + barAge +
", barDate=" + barDate +
'}';
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import java.util.*;
/**
* Created by 2IntelliJ IDEA.
*
* @author leizhimin 11-11-22 上午9:45
*/
public class Test {
private static SerializeConfig mapping = new SerializeConfig(); static {
mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
}
public static void main(String[] args) {
Foo f1 = new Foo();
Date date = new Date();
String text = JSON.toJSONString(date, mapping);
System.out.println(text);
System.out.println(JSON.toJSONString(f1, true));
String x2 =JSON.toJSONString(f1, mapping);
System.out.println(x2);
}
public static void json2List(){
//List -> JSON array
List<Bar> barList = new ArrayList<Bar>();
barList.add(new Bar());
barList.add(new Bar());
barList.add(new Bar());
String json= JSON.toJSONString(barList, true);
System.out.println(json);
//JSON array -> List
List<Bar> barList1 = JSON.parseArray(json,Bar.class);
for (Bar bar : barList1) {
System.out.println(bar.toString());
}
}
public static void json2Map(){
//Map -> JSON
Map<String,Bar> map = new HashMap<String, Bar>();
map.put("a",new Bar());
map.put("b",new Bar());
map.put("c",new Bar());
String json = JSON.toJSONString(map,true);
System.out.println(json);
//JSON -> Map
Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json);
for (String key : map1.keySet()) {
System.out.println(key+":"+map1.get(key));
}
}
public static void array2JSON(){
String[] arr_String = {"a","b","c"};
String json_arr_String = JSON.toJSONString(arr_String,true);
System.out.println(json_arr_String);
JSONArray jsonArray = JSON.parseArray(json_arr_String);
for (Object o : jsonArray) {
System.out.println(o);
}
System.out.println(jsonArray);
}
public static void array2JSON2(){
Bar[] arr_Bar = {new Bar(),new Bar(),new Bar()};
String json_arr_Bar = JSON.toJSONString(arr_Bar,true);
System.out.println(json_arr_Bar);
JSONArray jsonArray = JSON.parseArray(json_arr_Bar);
for (Object o : jsonArray) {
System.out.println(o);
}
System.out.println(jsonArray);
}
public static void map2JSON(){
Map map = new HashMap();
map.put("a","aaa");
map.put("b","bbb");
map.put("c","ccc");
String json=JSON.toJSONString(map);
System.out.println(json);
Map map1 = JSON.parseObject(json);
for (Object o : map.entrySet()) {
Map.Entry<String,String> entry = (Map.Entry<String,String>)o;
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
}
}
{
"avBar":[{
"barAge":174398800,
"barDate":1322062233062,
"barName":"sss_0.62378174"
},{
"barAge":38938962,
"barDate":1322062233062,
"barName":"sss_0.36014742"
}],
"avString":["aaa","bbb","ccc"],
"avboolean":[true,false,true,true],
"avint":[1,2,3,4],
"bar":{
"barAge":1601495948,
"barDate":1322062233062,
"barName":"sss_0.46644872"
},
"dddd":1322062233046,
"listBar":[
{
"barAge":-1090113522,
"barDate":1322062233062,
"barName":"sss_0.83562374"
},
{
"barAge":478603194,
"barDate":1322062233062,
"barName":"sss_0.59483266"
},
{
"barAge":1118357669,
"barDate":1322062233062,
"barName":"sss_0.9961642"
}
],
"listString":[
"listString1",
"listString2",
"listString3"
],
"map":{"x":"s11111x","y":"s22222y","z":"s33333z"},
"v_Date":1322062233046,
"vboolean":false,
"vbyte":64,
"vchar":"x",
"vdouble":22.203,
"vfloat":12.1,
"vint":65535,
"vlong":9999999,
"vshort":128
}
{"avBar":[{"barAge":174398800,"barDate":"2011-11-23 23:30:33","barName":"sss_0.62378174"},{"barAge":38938962,"barDate":"2011-11-23 23:30:33","barName":"sss_0.36014742"}],"avString":["aaa","bbb","ccc"],"avboolean":[true,false,true,true],"avint":[1,2,3,4],"bar":{"barAge":1601495948,"barDate":"2011-11-23 23:30:33","barName":"sss_0.46644872"},"dddd":"2011-11-23 23:30:33","listBar":[{"barAge":-1090113522,"barDate":"2011-11-23 23:30:33","barName":"sss_0.83562374"},{"barAge":478603194,"barDate":"2011-11-23 23:30:33","barName":"sss_0.59483266"},{"barAge":1118357669,"barDate":"2011-11-23 23:30:33","barName":"sss_0.9961642"}],"listString":["listString1","listString2","listString3"],"map":{"x":"s11111x","y":"s22222y","z":"s33333z"},"v_Date":"2011-11-23 23:30:33","vboolean":false,"vbyte":64,"vchar":"x","vdouble":22.203,"vfloat":12.1,"vint":65535,"vlong":9999999,"vshort":128}
Process finished with exit code 0
强大的json工具:fastJson的更多相关文章
- 阿里巴巴Json工具-Fastjson讲解
Fastjson是阿里巴巴公司开源的速度最快的Json和对象转换工具,一个Java语言编写的JSON处理器. 1.遵循http://json.org标准,为其官方网站收录的参考实现之一.2.功能qia ...
- 高性能JSON工具-FastJson处理超大JSON文本
使用阿里开源类库FastJson,当需要处理超大JSON文本时,需要Stream API,在fastjson-1.1.32版本中开始提供Stream API.文档参考GitHub:https://gi ...
- Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
- Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
- [Java学习] JSON工具学习记录--FastJSON
最近做项目,总是与json打交道,在用了即可json工具后,个人认为fastJson最好用,很方便,API也清晰可见,所以记录下使用方法,给需要的人提供帮助.(部分摘抄自网络) 一.API入口 Fas ...
- JSON工具类库: alibaba/fastjson 使用记录
JSON工具类库: alibaba/fastjson 使用记录 一.了解JSON JSON标准规范中文文档: http://www.json.org/json-zh.html 最佳实践:http:// ...
- 强大的Java Json工具类
转自: https://blog.csdn.net/u014676619/article/details/49624165 import java.io.BufferedReader; import ...
- 不同Json工具对空串和NULL的序列号处理:net.sf.json 和 fastjson
目录 1.测试代码 2.测试结果: 3.总结: 4.注:Maven中引入net.sf.json的方式 net.sf.json 和 fastjson 对于空串和NULL的处理: 1.测试代码 packa ...
- Json工具类库之Gson实战笔记
日常接口的数据传输通常使用xml或者json来传递数据,xml较庞大但是描述数据能力十分出众,json数据结构较小而且支持ajax传输,xml在数据传输和解析资源占用都比较逊色于json.因此日常的接 ...
随机推荐
- bzoj 1875: [SDOI2009]HH去散步 -- 矩阵乘法
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走, ...
- 读书笔记_Effective_C++_条款四十:明智而审慎地使用多重继承
多重继承是一种比较复杂的继承关系,它意味着如果用户想要使用这个类,那么就要对它的父类也了如指掌,所以在项目中会带来可读性的问题,一般我们都会尽量选择用单继承去替代它. 使用多重继承过程容易碰到的问题就 ...
- [Windows核心编程]32bit程序在64bit操作系统下处理重定向细节[1]
这段时间,都在做Ring3层的普通32bit程序兼容64bit操作系统的代码修改,在此记录修改和学习心德.编程领域太广, 任何人经历有限,本人不是专家,所以我一贯原则是: 用到的时候,才去研究,在去记 ...
- 浅入浅出---JQuery究竟是什么?
学习完了JQuery之后.我便感觉云里雾里的,JQuery究竟是什么.朦朦胧胧感觉到JQuery应该是javascript函数的封装.就应该像WinForm窗口应用程序中能够调用的系统函数,据之前所学 ...
- 图片未完成加载显示loading
<html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:lef ...
- JS浮点数运算
//乘法函数,用来得到精确的乘法结果 //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显.这个函数返回较为精确的乘法结果. //调用:accMul(arg1,arg2) ...
- 【docker】linux系统centOS 7上安装docker
要求: 一个centOS 7系统 虚拟就上安装CentOS 7步骤 本文操作在本机上使用xshell连接虚拟机上的centOS 7进行操作 1.Docker 要求 CentOS 系统的内核版本高于 ...
- FFmpeg的H264编码有内存泄漏吗??!!!
靠,内存泄漏好严重.开始怀疑是自己代码问题,调试了半天,又反复改写和优化代码,还是泄漏严重. 拿网上现成的FFMPEG H264编码的范例来测试,同样泄漏很严重. 百度了一下,有很多人遇到同样的问题, ...
- style="visibility: hidden"和 style=“display:none”之间的区别
style=“display:none” 隐藏页面元素: <html> <head> <script type="text/javascript"&g ...
- css3 3d展示中rotate()介绍与简单实现
最近在了解css3的3d动画效果,学习发现,css3中的3d效果实现还是很好玩的,现在我给你大家简单的介绍一下css3中3d效果的实现. 我也只是一个初学者,如果在博客中写的不对的地方欢迎指正. 好了 ...