FastJson的使用

FastJson据说是目前为止最快的JSON库,好吧,他说是就是了。

FastJson 的Wiki在这里:https://github.com/alibaba/fastjson/wiki/

Quick_Start 在这里:https://github.com/alibaba/fastjson/wiki/Quit_Start_cn

 

FastJson的API非常简单:

1 String text = JSON.toJSONString(obj); //序列化
2 VO vo = JSON.parseObject("{...}", VO.class); //反序列化

 

接着,上菜,不,上代码:

这里需要注意一个核心思想:JSON中的键值对就是Java中的Map,JSON中的数组就是Java中的List

 1 package com.owen.fastjson.bean;
 2 
 3 /**
 4  * POJO对象
 5  * 
 6  * @author Owen
 7  */
 8 public class UserInfo {
 9     
10     private String name;
11     
12     private int age;
13 
14     /**
15      * FastJSON要求:需要提供默认构造方法
16      */
17     public UserInfo() {}
18     
19     public UserInfo(String name, int age) {
20         this.name = name;
21         this.age = age;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public int getAge() {
33         return age;
34     }
35 
36     public void setAge(int age) {
37         this.age = age;
38     }
39     
40 }
  1 package com.owen.fastjson.app;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Date;
  5 import java.util.HashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import com.alibaba.fastjson.JSON;
 10 import com.alibaba.fastjson.TypeReference;
 11 import com.alibaba.fastjson.serializer.SerializerFeature;
 12 import com.owen.fastjson.bean.UserInfo;
 13 
 14 /**
 15  * 测试类
 16  * 
 17  * @author Owen
 18  */
 19 public class Main {
 20 
 21     /**
 22      * 简单序列化
 23      */
 24     private static void simpleTest() {
 25         UserInfo userInfo = new UserInfo("owen", 24);
 26         String jsonStr = JSON.toJSONString(userInfo);
 27         System.out.println(jsonStr);
 28     }
 29     
 30     /**
 31      * 复杂序列化
 32      */
 33     public static void complexTest() {
 34         Map<String, Object> map = new HashMap<String, Object>();
 35         map.put("username", "owen");
 36         map.put("age", 25);
 37         map.put("sex", "男");
 38         
 39         Map<String, Object> temp = new HashMap<String, Object>();
 40         temp.put("name", "jack");
 41         temp.put("age", 18);
 42         map.put("girinfo", temp);
 43 
 44         List<String> list = new ArrayList<String>();
 45         list.add("爬山");
 46         list.add("电影");
 47         list.add("旅游");
 48         map.put("hobby", list);
 49         
 50         String jsonStr = JSON.toJSONString(map);
 51         System.out.println(jsonStr);
 52     }
 53     
 54     /**
 55      * 日期序列化
 56      */
 57     public static void DateTest() {
 58         Date date = new Date();
 59         // 默认输出
 60         System.out.println("时间戳=" + JSON.toJSONString(date));
 61         // 默认的日期格式
 62         System.out.println("当前日期=" + JSON.toJSONString(date, SerializerFeature.WriteDateUseDateFormat));
 63         // 使用指定的日期格式
 64         System.out.println("当前日期=" + JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat));
 65     }
 66     
 67     /**
 68      * 简单反序列化
 69      */
 70     public static void simpleDeserializeTest() {
 71         String userInfoJSONStr = "{\"name\":\"owen\", \"age\":24}";
 72         UserInfo userInfo = JSON.parseObject(userInfoJSONStr, UserInfo.class);
 73         System.out.println("name = " + userInfo.getName() + ", age = " + userInfo.getAge());
 74     }
 75     
 76     /**
 77      * 泛型反序列化
 78      */
 79     public static void genericTypeDeserializeTest() {
 80         String jsonStr = "{\"user\":{\"name\":\"owen\", \"age\":24}";
 81         Map<String, UserInfo> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, UserInfo>>() {});
 82         System.out.println(map.get("user").getAge() + "\n");
 83         
 84         String jsonStr2 = "{\"user\":[{\"name\":\"owen\", \"age\":24}, {\"name\":\"jack\", \"age\":18}]";
 85         Map<String, List<UserInfo>> users = JSON.parseObject(jsonStr2, new TypeReference<Map<String, List<UserInfo>>>() {});
 86         for (UserInfo info : users.get("user")) {
 87             System.out.println("name=" + info.getName());
 88             System.out.println("age=" + info.getAge());
 89             System.out.println("------------------");
 90         }
 91     }
 92     
 93     /**
 94      * test
 95      */
 96     public static void main(String[] args) {
 97         genericTypeDeserializeTest();
 98     }
 99     
100 }

 

 

参考来源:

https://m.oschina.net/blog/369875

http://blog.csdn.net/u012083681/article/details/17514617

posted @ 2015-04-13 11:02  红尘炼心  阅读(305)  评论(0编辑  收藏  举报