1、前言java
Json序列化和反序列化工做中會時經常使用到,也是目前數據交互的經常使用格式,Rest風格的接口加上json格式的數據交互,真的是天做之合。json
目前Json字符與Json對象的相互轉換方式有不少,接下來將爲你們一一介紹下,感興趣的朋友能夠參考下哈,但願能夠幫助到你app
2、工具介紹工具
此次爲你們介紹經常使用的三種工具this
一、Fastjson:http://fastjson.codeplex.com/google
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency>
二、Jackson:http://jackson.codehaus.org/code
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.0.rc1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.0.rc1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0.rc1</version> </dependency>
三、Gson:http://code.google.com/p/google-gson/xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency>
3、工具使用對象
一、首先建立兩個類,一個Student 類,一個Teacher類 代碼以下:blog
Student.java
package com.zsy.model; public class Student { String name; int age; String sex; Teacher teacher; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }
Teacher.java
package com.zsy.model; public class Teacher { String name; String Sex; int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return Sex; } public void setSex(String sex) { Sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
二、使用方法
import java.io.IOException; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.zsy.model.Student; import com.zsy.model.Teacher; public class Test { public static void main(String args[]) throws IOException{ Student student=new Student(); student.setName("小明"); student.setAge(10); student.setSex("男"); Teacher teacher=new Teacher(); teacher.setName("lisa"); teacher.setAge(35); teacher.setSex("女"); student.setTeacher(teacher); //jackson-core obj->json ObjectMapper mapper=new ObjectMapper(); String json=mapper.writeValueAsString(student); System.out.println(json); //jsckson json->obj Student studentObj=mapper.readValue(json, Student.class); System.out.println(studentObj.getName()+"======="); //GSON 序列化 obj->json String jsonstr = new Gson().toJson(student); System.out.println(jsonstr); //GSON 反序列化 json->obj Student myobj = new Gson().fromJson(jsonstr, Student.class); System.out.println(myobj.getName()+"======="); //alibaba fastJson String jsons=JSON.toJSONString(studentObj); System.out.println(jsons); Student stu=JSON.parseObject(json, Student.class); System.out.println(stu.getName()+"=========="); } }