FastJson 介绍

Fast json 介绍

Fastjson是一个 Java 语言编写的JSON 处理器 , 阿里巴巴 公司开发。
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
5、具有超高的 性能 java 世界里没有其他的 json 库能够和 fastjson 可相比了。

Fastjson的最主要的使用入口是 com.alibaba.fastjson.JSON

import com.alibaba.fastjson.JSON;

public static final Object parse(String text); // 把 JSON 文本 parseJSONObject 或者 JSONArray
public static final JSONObject parseObject(String text);  // JSON 文本 parseJSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把 JSON 文本 parseJavaBean
public static final JSONArray parseArray(String text); // 把 JSON 文本 parseJSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把 JSON 文本 parseJavaBean 集合
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

代码示例用到类Foo Bar

 

FastJson 介绍
  1 import java.util.*;
  2 
  3 public class Foo {
  4     private String vString = "vStringhehhehe";
  5     private char vchar = ‘x‘;
  6 
  7     private byte vbyte = 64;
  8     private short vshort = 128;
  9     private int vint = 65535;
 10     private long vlong = 9999999L;
 11 
 12     private float vfloat = 12.1f;
 13     private double vdouble = 22.203d;
 14 
 15     private boolean vboolean = false;
 16 
 17     // private Date vdate = new Date();
 18     private Date dddd = new Date();
 19     private Date vDate = new Date();
 20     private Date v_Date = new Date();
 21     private Object vnull = null;
 22 
 23     private String[] avString = { "aaa", "bbb", "ccc" };
 24     private int[] avint = { 1, 2, 3, 4 };
 25     private boolean[] avboolean = { true, false, true, true };
 26 
 27     private List<String> listString = new ArrayList<String>();
 28     private Map<String, String> map = new HashMap<String, String>();
 29 
 30     private Bar bar = new Bar();
 31     private Bar[] avBar = { new Bar(), new Bar() };
 32     private List<Bar> listBar = new ArrayList<Bar>();
 33 
 34     {
 35         listString.add("listString1");
 36         listString.add("listString2");
 37         listString.add("listString3");
 38 
 39         map.put("x", "s11111x");
 40         map.put("y", "s22222y");
 41         map.put("z", "s33333z");
 42 
 43         listBar.add(new Bar());
 44         listBar.add(new Bar());
 45         listBar.add(new Bar());
 46     }
 47 
 48     public String getvString() {
 49         return vString;
 50     }
 51 
 52     public void setvString(String vString) {
 53         this.vString = vString;
 54     }
 55 
 56     public char getVchar() {
 57         return vchar;
 58     }
 59 
 60     public void setVchar(char vchar) {
 61         this.vchar = vchar;
 62     }
 63 
 64     public byte getVbyte() {
 65         return vbyte;
 66     }
 67 
 68     public void setVbyte(byte vbyte) {
 69         this.vbyte = vbyte;
 70     }
 71 
 72     public short getVshort() {
 73         return vshort;
 74     }
 75 
 76     public void setVshort(short vshort) {
 77         this.vshort = vshort;
 78     }
 79 
 80     public int getVint() {
 81         return vint;
 82     }
 83 
 84     public void setVint(int vint) {
 85         this.vint = vint;
 86     }
 87 
 88     public long getVlong() {
 89         return vlong;
 90     }
 91 
 92     public void setVlong(long vlong) {
 93         this.vlong = vlong;
 94     }
 95 
 96     public float getVfloat() {
 97         return vfloat;
 98     }
 99 
100     public void setVfloat(float vfloat) {
101         this.vfloat = vfloat;
102     }
103 
104     public double getVdouble() {
105         return vdouble;
106     }
107 
108     public void setVdouble(double vdouble) {
109         this.vdouble = vdouble;
110     }
111 
112     public boolean isVboolean() {
113         return vboolean;
114     }
115 
116     public void setVboolean(boolean vboolean) {
117         this.vboolean = vboolean;
118     }
119 
120     public Date getDddd() {
121         return dddd;
122     }
123 
124     public void setDddd(Date dddd) {
125         this.dddd = dddd;
126     }
127 
128     public Date getV_Date() {
129         return v_Date;
130     }
131 
132     public void setV_Date(Date v_Date) {
133         this.v_Date = v_Date;
134     }
135 
136     public Date getvDate() {
137         return vDate;
138     }
139 
140     public void setvDate(Date vDate) {
141         this.vDate = vDate;
142     }
143 
144     public Object getVnull() {
145         return vnull;
146     }
147 
148     public void setVnull(Object vnull) {
149         this.vnull = vnull;
150     }
151 
152     public String[] getAvString() {
153         return avString;
154     }
155 
156     public void setAvString(String[] avString) {
157         this.avString = avString;
158     }
159 
160     public int[] getAvint() {
161         return avint;
162     }
163 
164     public void setAvint(int[] avint) {
165         this.avint = avint;
166     }
167 
168     public boolean[] getAvboolean() {
169         return avboolean;
170     }
171 
172     public void setAvboolean(boolean[] avboolean) {
173         this.avboolean = avboolean;
174     }
175 
176     public List<String> getListString() {
177         return listString;
178     }
179 
180     public void setListString(List<String> listString) {
181         this.listString = listString;
182     }
183 
184     public Map<String, String> getMap() {
185         return map;
186     }
187 
188     public void setMap(Map<String, String> map) {
189         this.map = map;
190     }
191 
192     public Bar getBar() {
193         return bar;
194     }
195 
196     public void setBar(Bar bar) {
197         this.bar = bar;
198     }
199 
200     public Bar[] getAvBar() {
201         return avBar;
202     }
203 
204     public void setAvBar(Bar[] avBar) {
205         this.avBar = avBar;
206     }
207 
208     public List<Bar> getListBar() {
209         return listBar;
210     }
211 
212     public void setListBar(List<Bar> listBar) {
213         this.listBar = listBar;
214     }
215 }
Foo.java

 

FastJson 介绍
 1 import com.alibaba.fastjson.JSON; 
 2 import com.alibaba.fastjson.serializer.SerializeConfig; 
 3 import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; 
 4 
 5 import java.util.Date; 
 6 import java.util.Random; 
 7 
 8 public class Bar { 
 9         public static SerializeConfig mapping = new SerializeConfig(); 
10         private String barName; 
11         private int barAge; 
12         private Date barDate = new Date(); 
13         static { 
14                 mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); 
15         } 
16         { 
17                 Random r = new Random(); 
18                 barName = "sss_"+String.valueOf(r.nextFloat()); 
19                 barAge = r.nextInt(10); 
20         } 
21 
22         public static void main(String[] args) { 
23                 Object obj = JSON.toJSON(new Bar()); 
24                 String x1 = JSON.toJSONString(new Bar(), true); 
25                 System.out.println(x1); 
26                 String x2 = JSON.toJSONString(new Bar(), mapping); 
27                 System.out.println(x2); 
28         } 
29 
30         public String getBarName() { 
31                 return barName; 
32         } 
33 
34         public void setBarName(String barName) { 
35                 this.barName = barName; 
36         } 
37 
38         public int getBarAge() { 
39                 return barAge; 
40         } 
41 
42         public void setBarAge(int barAge) { 
43                 this.barAge = barAge; 
44         } 
45 
46         public Date getBarDate() { 
47                 return barDate; 
48         } 
49 
50         public void setBarDate(Date barDate) { 
51                 this.barDate = barDate; 
52         } 
53 
54         @Override 
55         public String toString() { 
56                 return "Bar{" + 
57                                 "barName=‘" + barName + ‘\‘‘ + 
58                                 ", barAge=" + barAge + 
59                                 ", barDate=" + barDate + 
60                                 ‘}‘; 
61         } 
62 }
Bar.java
FastJson 介绍
 1 import com.alibaba.fastjson.JSON; 
 2 import com.alibaba.fastjson.JSONArray; 
 3 import com.alibaba.fastjson.serializer.SerializeConfig; 
 4 import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; 
 5 
 6 import java.util.*; 
 7 
 8 public class Main { 
 9         private static SerializeConfig mapping = new SerializeConfig(); static { 
10                 mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); 
11         } 
12 
13         public static void main(String[] args) { 
14             Test.json2Bean();                            
15 //            Test.json2List();
16 //            Test.json2Map();
17 //            Test.array2JSON();
18 //            Test.array2JSON2();
19 //            Test.map2JSON();
20         } 
21 
22         public static void json2Bean(){ 
23                 //Bean->JSON
24             Bar bar1 = new Bar();
25             String str = JSON.toJSONString(bar,true);
26             System.out.println(str);
27             Bar bar2 = new Bar();
28             bar2 = JSON.parseObject(str,Bar.class);
29             System.out.println(bar2);
30 
31         }
32 
33         public static void json2List(){ 
34                 //List -> JSON array 
35                 List<Bar> barList = new ArrayList<Bar>(); 
36                 barList.add(new Bar()); 
37                 barList.add(new Bar()); 
38                 barList.add(new Bar()); 
39                 String json= JSON.toJSONString(barList, true); 
40                 System.out.println(json); 
41                 //JSON array -> List 
42                 List<Bar> barList1 = JSON.parseArray(json,Bar.class); 
43                 for (Bar bar : barList1) { 
44                         System.out.println(bar.toString()); 
45                 } 
46         } 
47 
48         public static void json2Map(){ 
49                 //Map -> JSON 
50                 Map<String,Bar> map = new HashMap<String, Bar>(); 
51                 map.put("a",new Bar()); 
52                 map.put("b",new Bar()); 
53                 map.put("c",new Bar()); 
54                 String json = JSON.toJSONString(map,true); 
55                 System.out.println(json); 
56                 //JSON -> Map 
57                 Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); 
58                 for (String key : map1.keySet()) { 
59                         System.out.println(key+":"+map1.get(key)); 
60                 } 
61         } 
62 
63         public static void array2JSON(){ 
64                 String[] arr_String    = {"a","b","c"}; 
65                 String json_arr_String = JSON.toJSONString(arr_String,true); 
66                 System.out.println(json_arr_String); 
67                 JSONArray jsonArray = JSON.parseArray(json_arr_String); 
68                 for (Object o : jsonArray) { 
69                         System.out.println(o); 
70                 } 
71                 System.out.println(jsonArray); 
72         } 
73         public static void array2JSON2(){ 
74                 Bar[] arr_Bar    = {new Bar(),new Bar(),new Bar()}; 
75                 String json_arr_Bar = JSON.toJSONString(arr_Bar,true); 
76                 System.out.println(json_arr_Bar); 
77                 JSONArray jsonArray = JSON.parseArray(json_arr_Bar); 
78                 for (Object o : jsonArray) { 
79                         System.out.println(o); 
80                 } 
81                 System.out.println(jsonArray); 
82         } 
83 
84         public static void map2JSON(){ 
85                 Map map = new HashMap(); 
86                 map.put("a","aaa"); 
87                 map.put("b","bbb"); 
88                 map.put("c","ccc"); 
89                 String json=JSON.toJSONString(map); 
90                 System.out.println(json); 
91                 Map map1 = JSON.parseObject(json); 
92                 for (Object o : map.entrySet()) { 
93                         Map.Entry<String,String> entry = (Map.Entry<String,String>)o; 
94                         System.out.println(entry.getKey()+"--->"+entry.getValue()); 
95                 } 
96         } 
97 }
Main

 

 fastjson-1.1.39.jar:http://pan.baidu.com/s/1mg4tbAS

 fastjson-1.1.39-sources.jar:http://pan.baidu.com/s/1mg4YURA

FastJson 介绍,布布扣,bubuko.com

FastJson 介绍

上一篇:jquery中html()、text()、val()的区别


下一篇:在MyEclipse8.6中设置jQuery自动提示 - 肖飞figo的云计算专栏 - 博客频道 - CSDN.NET