在项目中经常用到json格式的数据转换与解析,先前写过一些小例子,现在整理下,以备后用和帮助后来者。
言归正传:
使用到的jar包 :json-lib-2.4-jdk15.jar,当然你也可以用自己版本的,一般都没啥问题。
一、用于测试数据的类:
1.Student
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package com.json.gson; 6 7 import java.util.List; 8 9 /** 10 * gsonTest测试类 : 学生 11 * 12 * @author zhengze.su 13 */ 14 public class Student { 15 16 private String name; 17 private String sex; 18 private int age; 19 private String address; 20 private List<Hobby> hobbyList; 21 private List<Course> courseList; 22 23 /** 24 * @return the name 25 */ 26 public String getName() { 27 return name; 28 } 29 30 /** 31 * @param name the name to set 32 */ 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 /** 38 * @return the sex 39 */ 40 public String getSex() { 41 return sex; 42 } 43 44 /** 45 * @param sex the sex to set 46 */ 47 public void setSex(String sex) { 48 this.sex = sex; 49 } 50 51 /** 52 * @return the age 53 */ 54 public int getAge() { 55 return age; 56 } 57 58 /** 59 * @param age the age to set 60 */ 61 public void setAge(int age) { 62 this.age = age; 63 } 64 65 /** 66 * @return the address 67 */ 68 public String getAddress() { 69 return address; 70 } 71 72 /** 73 * @param address the address to set 74 */ 75 public void setAddress(String address) { 76 this.address = address; 77 } 78 79 /** 80 * @return the hobbyList 81 */ 82 public List<Hobby> getHobbyList() { 83 return hobbyList; 84 } 85 86 /** 87 * @param hobbyList the hobbyList to set 88 */ 89 public void setHobbyList(List<Hobby> hobbyList) { 90 this.hobbyList = hobbyList; 91 } 92 93 /** 94 * @return the courseList 95 */ 96 public List<Course> getCourseList() { 97 return courseList; 98 } 99 100 /** 101 * @param courseList the courseList to set 102 */ 103 public void setCourseList(List<Course> courseList) { 104 this.courseList = courseList; 105 } 106 }