Java 用gson生成json資料
阿新 • • 發佈:2019-02-16
{
"result": 1,
"personData": [
{
"name": "Tom Green",
"age": 22,
"url": "https://img.alicdn.com/imgextra/i2/2578685019/TB21XA7iXXXXXbxXpXXXXXXXXXX_!!2578685019.jpg",
"schoolInfo": [
{
"school_name": "Cambridge"
},
{
"school_name": "Oxford"
}
]
},
{
"name": "Bill Gates",
"age": 66,
"url": "https://img.alicdn.com/tps/TB1PebaLXXXXXX3XpXXXXXXXXXX-990-500.jpg",
"schoolInfo": [
{
"school_name ": "北京大學"
},
{
"school_name": "清華大學"
}
]
}
]
}
Google gson地址:https://github.com/google/gson,將jar包下載後匯入到java工程中即可。我們來分析一下上述json資料,包括兩個元素,第一個是result,int型資料,第二個元素是personData,是一個物件(假設是Person物件)的集合,其中這個Person物件又包含了一個schoolInfo物件的集合。每個schoolInfo物件中只包含了一個school_name屬性。下面新建一個Java工程進行上述json資料的生成。
1 SchoolInfo.java
package www.imooc.com;
public class SchoolInfo {
private String school_name;
public String getSchool_name() {
return school_name;
}
public void setSchool_name(String school_name) {
this.school_name = school_name;
}
}
Person.java
package www.imooc.com;
import java.util.List;
public class Person {
private String name;
private int age;
private String url;
private List<SchoolInfo> schoolInfo;//SchoolInfo是一個SchoolInfo物件的集合
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 getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<SchoolInfo> getSchoolInfo() {
return schoolInfo;
}
public void setSchoolInfo(List<SchoolInfo> schoolInfo) {
this.schoolInfo = schoolInfo;
}
}
Result.java
package www.imooc.com;
import java.util.List;
/*
* Result包括一個int型別資料,一個Person類物件的集合
* */
public class Result {
private int result;
private List<Person> personData;
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public List<Person> getPersonData() {
return personData;
}
public void setPersonData(List<Person> personData) {
this.personData = personData;
}
}
2 新建TestJson.java,生成json
package www.imooc.com;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
public class TestJson {
public static void main(String[] args) {
//設定result的資料,包括一個int資料和一個Person物件的集合
Result result = new Result();
result.setResult(1);
List<Person> listPersons = new ArrayList<Person>();
//設定result類中的第一個Person物件,包括name,age,url和一個SchoolInfo物件的集合
Person person1 = new Person();
person1.setName("Tom Green");
person1.setAge(22);
person1.setUrl("https://img.alicdn.com/imgextra/i2/2578685019/TB21XA7iXXXXXbxXpXXXXXXXXXX_!!2578685019.jpg");
//設定Person類中的SchoolInfo,包括一個school_name
List<SchoolInfo> listSchoolInfo1 = new ArrayList<SchoolInfo>();
SchoolInfo schoolInfo1 = new SchoolInfo();
schoolInfo1.setSchool_name("Cambridge");
SchoolInfo schoolInfo2 = new SchoolInfo();
schoolInfo2.setSchool_name("Oxford");
listSchoolInfo1.add(schoolInfo1);
listSchoolInfo1.add(schoolInfo2);
person1.setSchoolInfo(listSchoolInfo1);//將SchoolInfo的物件集合新增到Person中
//設定result類中的第二個Person物件,包括name,age,url和一個SchoolInfo物件的集合
Person person2 = new Person();
person2.setName("Bill Gates");
person2.setAge(66);
person2.setUrl("https://img.alicdn.com/tps/TB1PebaLXXXXXX3XpXXXXXXXXXX-990-500.jpg");
//設定Person類中的SchoolInfo,包括一個school_name
List<SchoolInfo> listSchoolInfo2 = new ArrayList<SchoolInfo>();
SchoolInfo schoolInfo3 = new SchoolInfo();
schoolInfo3.setSchool_name("北京大學");
SchoolInfo schoolInfo4 = new SchoolInfo();
schoolInfo4.setSchool_name("清華大學");
listSchoolInfo2.add(schoolInfo3);
listSchoolInfo2.add(schoolInfo4);
person2.setSchoolInfo(listSchoolInfo2);//將SchoolInfo的物件集合新增到Person中
listPersons.add(person1);//將Person物件的集合新增到Result中
listPersons.add(person2);//將Person物件的集合新增到Result中
result.setPersonData(listPersons);
Gson gson = new Gson();
System.out.println(gson.toJson(result));//將上述設定的程式碼轉為json
}
}