【简介】
json是一种数据格式,便于数据传输、存储、交换
gson是一种组件库,可以把java对象数据转换成json数据格式
【Json串的解析和处理】
实际开发过程中我们经常会遇到Json串的解析问题。下面做一下举例分析[{"tableName":"students","tableData":[{"id":1,"name":"宝强","birthDay":"Jun 22, 2016 9:54:49 PM"},
{"id":2,"name":"马蓉","birthDay":"Jun 22, 2016 9:54:49 PM"},
{"id":3,"name":"宋喆","birthDay":"Jun 22, 2016 9:54:49 PM"}]},
{"tableName":"teachers","tableData":[{"id":1,"name":"刘老师","title":"教授"},
{"id":2,"name":"王老师","title":"讲师"}]}]
请看本文是如何处理的吧:
实体类:
- import java.util.Date;
- public class Student {
- private int id;
- private String name;
- private Date birthDay;
- 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 Date getBirthDay() {
- return birthDay;
- }
- public void setBirthDay(Date birthDay) {
- this.birthDay = birthDay;
- }
- @Override
- public String toString() {
- return "Student [birthDay=" + birthDay + ", id=" + id + ", name="
- + name + "]";
- }
- }
注意这里定义了一个TableData实体类:
- public class Teacher {
- private int id;
- private String name;
- private String title;
- 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 String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- @Override
- public String toString() {
- return "Teacher [id=" + id + ", name=" + name + ", title=" + title
- + "]";
- }
- }
测试类
- import java.util.List;
- public class TableData {
- private String tableName;
- private List tableData;
- public String getTableName() {
- return tableName;
- }
- public void setTableName(String tableName) {
- this.tableName = tableName;
- }
- public List getTableData() {
- return tableData;
- }
- public void setTableData(List tableData) {
- this.tableData = tableData;
- }
- }
(仔细看将json转回为对象的实现,这里经过两次转化,第一次转回的结果是map不是我们所期望的对象,
对map再次转为json后再转为对象,我引用的是Gson2.1的jar处理正常,好像使用Gson1.6的jar会报错,所以建议用最新版本)
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- public class GsonTest5 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // 对象转为Json-->start
- Student student1 = new Student();
- student1.setId(1);
- student1.setName("宝强");
- student1.setBirthDay(new Date());
- Student student2 = new Student();
- student2.setId(2);
- student2.setName("马蓉");
- student2.setBirthDay(new Date());
- Student student3 = new Student();
- student3.setId(3);
- student3.setName("宋喆");
- student3.setBirthDay(new Date());
- List<Student> stulist = new ArrayList<Student>();
- stulist.add(student1);
- stulist.add(student2);
- stulist.add(student3);
- Teacher teacher1 = new Teacher();
- teacher1.setId(1);
- teacher1.setName("刘老师");
- teacher1.setTitle("教授");
- Teacher teacher2 = new Teacher();
- teacher2.setId(2);
- teacher2.setName("王老师");
- teacher2.setTitle("讲师");
- List<Teacher> teacherList = new ArrayList<Teacher>();
- teacherList.add(teacher1);
- teacherList.add(teacher2);
- TableData td1 = new TableData();
- td1.setTableName("students");
- td1.setTableData(stulist);
- TableData td2 = new TableData();
- td2.setTableName("teachers");
- td2.setTableData(teacherList);
- List<TableData> tdList = new ArrayList<TableData>();
- tdList.add(td1);
- tdList.add(td2);
- Gson gson = new Gson();
- String s = gson.toJson(tdList);
- System.out.println(s);
- // 结果:[{"tableName":"students","tableData":
- [{"id":1,"name":"宝强","birthDay":"Oct 22, 2016 10:44:16 AM"},
- {"id":2,"name":"马蓉","birthDay":"Oct 22, 2016 10:44:16 AM"},
- {"id":3,"name":"宋喆","birthDay":"Oct 22, 2016 10:44:16 AM"}]},
- {"tableName":"teachers","tableData":[{"id":1,"name":"刘老师","title":"教授"},
- {"id":2,"name":"王老师","title":"讲师"}]}]
- // 对象转为Json-->end
- // /
- // 将json转为数据-->start
- List<TableData> tableDatas2 = gson.fromJson(s,
- new TypeToken<List<TableData>>() {
- }.getType());
- for (int i = 0; i < tableDatas2.size(); i++) {
- TableData entityData = tableDatas2.get(i);
- String tableName = entityData.getTableName();
- List tableData = entityData.getTableData();
- String s2 = gson.toJson(tableData);
- // System.out.println(s2);
- // System.out.println(entityData.getData());
- if (tableName.equals("students")) {
- System.out.println("students");
- List<Student> retStuList = gson.fromJson(s2,
- new TypeToken<List<Student>>() {
- }.getType());
- for (int j = 0; j < retStuList.size(); j++) {
- System.out.println(retStuList.get(j));
- }
- } else if (tableName.equals("teachers")) {
- System.out.println("teachers");
- List<Teacher> retTchrList = gson.fromJson(s2,
- new TypeToken<List<Teacher>>() {
- }.getType());
- for (int j = 0; j < retTchrList.size(); j++) {
- System.out.println(retTchrList.get(j));
- }
- }
- }
- // Json转为对象-->end
- }
- }
输出结果:
- [{"tableName":"students","tableData":
- [{"id":1,"name":"宝强","birthDay":"Oct 22, 2012 10:04:12 PM"},
- {"id":2,"name":"马蓉","birthDay":"Oct 22, 2012 10:04:12 PM"},
- {"id":3,"name":"宋喆","birthDay":"Oct 22, 2012 10:04:12 PM"}]},
- {"tableName":"teachers","tableData":[{"id":1,"name":"刘老师","title":"教授"},
- {"id":2,"name":"王老师","title":"讲师"}]}]
- students
- Student [birthDay=Fri Oct 22 22:04:12 CST 2016, id=1, name=宝强]
- Student [birthDay=Fri Oct 22 22:04:12 CST 2016, id=2, name=马蓉]
- Student [birthDay=Fri Oct 22 22:04:12 CST 2016, id=3, name=宋喆]
- teachers
- Teacher [id=1, name=刘老师, title=教授]
- Teacher [id=2, name=王老师, title=讲师]