Fastjson是阿里巴巴公司開源的速度最快的Json和對象轉換工具,一個Java語言編寫的JSON處理器。java
一、遵循http://json.org標準,爲其官方網站收錄的參考實現之一。
二、功能qiang打,支持JDK的各類類型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
三、無依賴,不須要例外額外的jar,可以直接跑在JDK上。
四、開源,使用Apache License 2.0協議開源。http://code.alibabatech.com/wiki/display/FastJSON/Homejson
二、主要的使用入口
數組
Fastjson API入口類是com.alibaba.fastjson.JSON,經常使用的序列化操做均可以在JSON類上的靜態方法直接完成。
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。
三、有關類庫的一些說明
SerializeWriter:至關於StringBuffer
JSONArray:至關於List<Object>
JSONObject:至關於Map<String, Object>
JSON反序列化沒有真正數組,本質類型都是List<Object>
四、fastjson還有不少很高級的特性,好比支持註解、支持全類型序列化,這些都是很好的特性,功能強大,再也不講解。服務器
這是關於FastJson的一個使用Demo,在Java環境下驗證的工具
- import java.util.ArrayList;
- import java.util.List;
-
- class Group{
- private int id;
- private String name;
- private List<User> list = new ArrayList<User>();
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public List<User> getList() {
- return list;
- }
- public void setList(List<User> list) {
- this.list = list;
- }
- }
- class Weibo {
- private String id;
- private String city;
-
- public Weibo(String id, String city) {
- this.id = id;
- this.city = city;
- }
-
- public Weibo() {
- }
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getCity() {
- return city;
- }
-
- public void setCity(String city) {
- this.city = city;
- }
- }
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- public class JsonDemo {
-
- public static void main(String[] args) {
-
- json2JsonObjectM1();
- json2BeanM2();
- json2JSONArrayM3();
- json2JavaBeanM4();
- bean2JsonM5();
- bean2JSONObjectM6();
- complexExampleM7();
- complex2JsonM8();
- complexMap2JsonM9();
- }
-
-
- private static void json2JsonObjectM1() {
-
- String s = "{\"name\":\"liuzhao\"}";
-
- JSONObject object = JSON.parseObject(s);
-
- System.out.println(object.get("name"));
-
- }
-
-
- private static void json2BeanM2() {
- String s = "{\"id\":\"0375\",\"city\":\"平頂山\"}";
-
- Weibo weibo = JSON.parseObject(s, Weibo.class);
- System.out.println(weibo.getId());
- System.out.println(weibo.getCity());
-
-
- }
-
-
-
- private static void complexMap2JsonM9() {
- Group group = new Group();
- group.setId(1);
- group.setName("group");
-
- User user1 = new User();
- user1.setId(2);
- user1.setName("user1");
-
- User user2 = new User();
- user2.setId(3);
- user2.setName("user2");
- group.getList().add(user1);
- group.getList().add(user2);
-
- Map<Integer, Object> map = new HashMap<Integer,Object>();
- map.put(1, "No.1");
- map.put(2, "No.2");
- map.put(3, group.getList());
-
- String jsonString = JSON.toJSONString(map);
- System.out.println(jsonString);
-
- }
-
-
- private static void complex2JsonM8() {
- Group group = new Group();
- group.setId(1);
- group.setName("group");
-
- User user1 = new User();
- user1.setId(2);
- user1.setName("user1");
-
- User user2 = new User();
- user2.setId(3);
- user2.setName("user2");
- group.getList().add(user1);
- group.getList().add(user2);
- String jsonString = JSON.toJSONString(group);
- System.out.println(jsonString);
-
- }
-
-
- private static void complexExampleM7() {
- String s = "{js:[{id:\"110000\",\"city\":\"北#001京市\"},{id:\"110000\",\"city\":\"北#002京市\"}"
- + ",{id:\"110000\",\"city\":\"北#002京市\"},{id:\"110000\",\"city\":\"北#002京市\"},"
- + "{id:\"110000\",\"city\":\"#006北#005京市\"},"
- + "{id:\"110000\",\"city\":\"北#002京市\"},"
- + "{id:\"110000\",\"city\":\"北#002京市\"},{id:\"120000\",\"city\":\"天#009津市\"}]}";
-
- JSONObject object = JSON.parseObject(s);
- Object jsonArray = object.get("js");
- System.out.println(jsonArray);
- List<Weibo> list = JSON.parseArray(jsonArray+"", Weibo.class);
- for (Weibo weibo : list) {
- System.out.println(weibo.getCity());
- }
- }
-
-
- private static void bean2JSONObjectM6() {
- Weibo weibo = new Weibo("0373", "洛陽");
- JSONObject json = (JSONObject) JSON.toJSON(weibo);
- System.out.println(json.get("id"));
-
- }
-
-
- private static void json2JavaBeanM4() {
- String s = "[{\"id\":\"0375\",\"city\":\"平頂山\"},{\"id\":\"0377\",\"city\":\"南陽\"}]";
- List<Weibo> list = JSON.parseArray(s, Weibo.class);
- for (Weibo weibo : list) {
- System.out.println(weibo.getCity());
- }
-
- }
-
-
- private static void json2JSONArrayM3() {
- String s = "[{\"id\":\"0375\",\"city\":\"平頂山\"},{\"id\":\"0377\",\"city\":\"南陽\"}]";
-
- JSONArray array = JSON.parseArray(s);
-
- String str = array.get(1)+"";
- System.out.println(array.get(0));
- JSONObject object = JSON.parseObject(str);
- System.out.println(object.get("id"));
-
- }
-
-
-
- private static void bean2JsonM5() {
- Weibo weibo = new Weibo("123456", "上海");
- String string = JSON.toJSONString(weibo);
- System.out.println(string);
-
- }
-
- }