8 fastJson的使用

Fastjson介绍

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?
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

 

如果你使用了Maven,maven repository配置如下:

 

<repository>
	<id>opensesame</id>
	<name>Alibaba OpenSource Repsoitory</name>
	<url>http://code.alibabatech.com/mvn/releases/</url>
	<snapshots>
		<enabled>false</enabled>
	</snapshots>
</repository>

 

pom.xml文件中加入依赖依赖:

 

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.9</version>
</dependency>

 

如果没有使用maven,可以直接下载:

 

Binary : http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1.jar
Source :http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1-sources.jar
Subversion : http://code.alibabatech.com/svn/fastjson/

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

 

import com.alibaba.fastjson.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。

 

使用例子:

两个基础类:

 1 public class Student {
 2     private int id;
 3 
 4     private String name;
 5 
 6     /**
 7      * 默认的构造方法必须不能省,不然不能通过json字符串解析成java对象
 8      */
 9     public Student() {
10     }
11 
12     public Student(int id, String name) {
13         this.id = id;
14         this.name = name;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 }
 1 public class Teacher {
 2     private int id;
 3 
 4     private String name;
 5 
 6     private List<Student> stus;
 7     /**
 8      * 默认的构造方法必须不能省,不然不能通过json字符串解析成java对象
 9      */
10     public Teacher() {
11     }
12 
13     public Teacher(int id, String name) {
14         this.id = id;
15         this.name = name;
16     }
17 
18     public int getId() {
19         return id;
20     }
21 
22     public void setId(int id) {
23         this.id = id;
24     }
25 
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public List<Student> getStus() {
35         return stus;
36     }
37 
38     public void setStus(List<Student> stus) {
39         this.stus = stus;
40     }
41 
42     public void addStudent(Student stu){
43         this.stus.add(stu);
44     }
45 
46     public void addStudents(List<Student> stus){
47         for (Student stu : stus) {
48             this.stus.add(stu);
49         }
50     }
51 
52     public void setStudents(List<Student> stus){
53         this.stus=stus;
54     }
55 }
 1 public class JsonTest1 {
 2     public static void main(String[] args) {
 3         Student stu=new Student(1,"one");
 4         //java实例转化成json字符串
 5         String jsonStr=test1(stu);
 6         print();
 7         //json字符串转成java实例
 8         stu=test2(jsonStr);
 9         print();
10         //集合转成json字符串
11         jsonStr=test3();
12         print();
13 
14         //复杂集合转成json字符串
15         test4();
16         print();
17 
18 
19 
20     }
21     //java实例转化成json字符串
22     public static String test1(Student stu){
23         String jsonStr=JSON.toJSONString(stu);
24         System.out.println(jsonStr);
25         return jsonStr;
26     }
27 
28     //json字符串转成java实例
29     public static Student test2(String jsonStr){
30         Student stu=JSON.parseObject(jsonStr,Student.class);
31         System.out.println("转换前json字符串:"+jsonStr);
32         System.out.println("转换后:id="+stu.getId()+",name="+stu.getName());
33         return stu;
34     }
35 
36     //集合转成json字符串
37     public static String test3(){
38         List<Student> list=new ArrayList<Student>();
39         for(int i=1;i<=3;i++){
40             list.add(new Student(i,"name "+i));
41         }
42         String jsonStr=JSON.toJSONString(list);
43         System.out.println(jsonStr);
44         return jsonStr;
45     }
46 
47     //复杂集合转成json字符串
48     public static String test4(){
49         List<Teacher> teaList = new ArrayList<Teacher>();
50         long time = System.currentTimeMillis();
51         for(int i=0;i<10;i++) {
52             Teacher teacher = new Teacher(i, "Teacher " + i);
53             List<Student> stus = new ArrayList<Student>();
54             for(int j = 0 ;j<4;j++) {
55                 Student s = new Student(j, "Student" + j);
56                 stus.add(s);
57             }
58             teacher.setStudents(stus);
59             teaList.add(teacher);
60         }
61         //true表示按照json格式输出
62         String jsonStr = JSON.toJSONString(teaList,true);
63         System.out.println(jsonStr);
64         return jsonStr;
65     }
66     public static void print(){
67         System.out.println("....................................");
68     }
69 }

输出结果:

{"id":1,"name":"one"}
....................................
转换前json字符串:{"id":1,"name":"one"}
转换后:id=1,name=one
....................................
[{"id":1,"name":"name 1"},{"id":2,"name":"name 2"},{"id":3,"name":"name 3"}]
....................................
[
    {
        "id":0,
        "name":"Teacher 0",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":1,
        "name":"Teacher 1",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":2,
        "name":"Teacher 2",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":3,
        "name":"Teacher 3",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":4,
        "name":"Teacher 4",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":5,
        "name":"Teacher 5",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":6,
        "name":"Teacher 6",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":7,
        "name":"Teacher 7",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":8,
        "name":"Teacher 8",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    },
    {
        "id":9,
        "name":"Teacher 9",
        "stus":[
            {
                "id":0,
                "name":"Student0"
            },
            {
                "id":1,
                "name":"Student1"
            },
            {
                "id":2,
                "name":"Student2"
            },
            {
                "id":3,
                "name":"Student3"
            }
        ]
    }
]
....................................

如果对类中有些字段不解析,可以通过SimplePropertyPreFilter来选取需要需要解析的属性,例子如下:

1  //类中有个别字段不解析
2     public static void test5(){
3         Student stu=new Student(1,"one");
4         SimplePropertyPreFilter filter=new SimplePropertyPreFilter(Student.class,"id");
5         String jsonStr=JSON.toJSONString(stu,filter);
6         System.out.println(jsonStr);
7     }

输出结果:

{"id":1}

把list集合的json字符串反序列为list集合:

 1     public static void test6(){
 2         List<Student> students = new ArrayList<Student>();
 3         for(int i=0;i<5;i++) {
 4             Student stu = new Student(i, "Student" + i);
 5             students.add(stu);
 6         }
 7         // 过滤哪些属性需要转换
 8 //      SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age");
 9 //      String jsonStu =JSON.toJSONString(students,filter);
10         String jsonStu =JSON.toJSONString(students);
11         System.out.println(jsonStu);
12 
13         List<Student> stu =JSON.parseObject(jsonStu, new TypeReference<List<Student>>(){});
14         for(int i=0;i<stu.size();i++)
15         {
16             System.out.println(stu.get(i));
17         }
18     }

代码输出:

[{"id":0,"name":"Student0"},{"id":1,"name":"Student1"},{"id":2,"name":"Student2"},{"id":3,"name":"Student3"},{"id":4,"name":"Student4"}]
com.main.etl.server.json.Student@60682430
com.main.etl.server.json.Student@551ffcc2
com.main.etl.server.json.Student@270b5037
com.main.etl.server.json.Student@57bf5ee7
com.main.etl.server.json.Student@54efbcb2

 

 

 

原文地址:https://www.cnblogs.com/yangh2016/p/5732201.html