几种常用的json序列化和反序列化工具介绍
一、前言
Json序列化和反序列化工作中会时常用到,也是目前数据交互的常用格式,Rest风格的接口加上json格式的数据交互,真的是天作之合。
目前Json字符与Json对象的相互转换方式有很多,接下来将为大家一一介绍下,感兴趣的朋友可以参考下哈,希望可以帮助到你
二、工具介绍
这次为大家介绍常用的三种工具
1、Fastjson:http://fastjson.codeplex.com/
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency>
2、Jackson:http://jackson.codehaus.org/
<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>
3、Gson:http://code.google.com/p/google-gson/
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency>
三、工具使用
1、首先创建两个类,一个Student 类,一个Teacher类 代码如下:
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; } }
2、使用方法
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()+"=========="); } }
所有博文内容,全部是自己一步一步操作出来的,请尊重版权,若转载请说明出处,谢谢。
不为失败找借口,只为成功找方法。欢迎各位和我一起遨游code世界!!!