fastJson API
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
fastJson API的更多相关文章
- FastJson一些常见方法(API):处理JSON变得好简单
fastjson是目前java语言中最快的json库,比自称最快的jackson速度要快,第三方独立测试结果说明比gson快大约6倍 JSON帮助类 这个可以做一个实例研究代码,也可以作为一个工具类 ...
- Java的Json解析包FastJson使用
阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...
- [转]fastjson
原文地址:http://www.cnblogs.com/zhenmingliu/archive/2011/12/29/2305775.html FastJSON是一个很好的java开源json工具类库 ...
- alibaba的FastJson(高性能JSON开发包),fastjson 使用demo
这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...
- 阿里巴巴Json工具-Fastjson讲解
Fastjson是阿里巴巴公司开源的速度最快的Json和对象转换工具,一个Java语言编写的JSON处理器. 1.遵循http://json.org标准,为其官方网站收录的参考实现之一.2.功能qia ...
- FastJSON 简介及其Map/JSON/String 互转
在日志解析,前后端数据传输交互中,经常会遇到 String 与 map.json.xml 等格式相互转换与解析的场景,其中 json 基本成为了跨语言.跨前后端的事实上的标准数据交互格式.应该来说各个 ...
- alibaba的FastJson(高性能JSON开发包)
这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...
- FastJson的基本用法----转
1. [文件] User.java ~ 243B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class User{ private int id; ...
- Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)
一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...
随机推荐
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!问题解决
用mac终端ssh连接Linux服务器,提示以下错误: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ WARNING: RE ...
- Powerdesign使用小技巧(转载)
1.做CDM模型的时候,因为开始定义ITEM的时候,没有注意把NAME和CODE全定义成一样的.结果后面想改的时候比较麻烦,一修改NAME结果CODE也变了.后来找到两个方法可以解决这个问题.A.Po ...
- JSP应用开发 -------- 电纸书(未完待续)
http://www.educity.cn/jiaocheng/j9415.html JSP程序员常用的技术 第1章 JSP及其相关技术导航 [本章专家知识导学] JSP是一种编程语言,也是一种动 ...
- 点滴积累【JS】---JS小功能(JS实现隐藏显示侧边栏,也就是分享栏的隐藏显示)
效果: 思路: 首先,利用计时器setInterval实现DIV的隐藏显示功能,然后在进行一个判断,之后在把要移动的相应距离进行一个参数传递,再根据参数判断出移动的方向也就是offsetLeft移动的 ...
- 点滴积累【C#】---Highcharts图形统计
效果: 思路: 后台获取数据!然后拼接为前台所要求的格式,再将拼接好的StringBuilder给了hidden控件! 然后前台获取JQuery获取Hidden的值,最后将值赋给图形! 代码: [前台 ...
- 转:SNMP 原理及配置简述
SNMP 原理及配置简述 转载 2016年01月13日 16:18:51 随着机器数量的增长,管理员不能像过去那样,一台台机器进行监控.解决问题,而需要借助各方工具进行统一监控和管理.利用SNMP,一 ...
- 基于numpy的随机数构造
class numpy.random.RandomState(seed=None) RandomState 是一个基于Mersenne Twister算法的伪随机数生成类 RandomState 包含 ...
- Oracle之比较NVARCHAR2字符串
一.引言 昨天遇到一个问题,一直想不通是为什么,我在pl/sql里面执行sql语句,选出字段值等于某个值的的行: '; 用select * from test;是可以查到该值的,但是用这条语句却没有结 ...
- string.erase()--已解决
在代码中需要实现这样一个功能,需要将[00000001]这个存储在string中的字符串的中括号去掉,首先想到的就是string.erase()这个函数.结果... 代码: #include < ...
- IDEA2017 导入 SVN上的 Myeclipse或Eclipse 项目
从SVN上检出项目,不用打开. 关闭IDEA后选择 import project,导入后按F4 (改成eclipse按键模式后 按ctrl alt shift F4)进入 project struct ...