package com.deppon.gson;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
class Student {
@Expose
private int id;
@Expose
@SerializedName("bir")
private String name;
private boolean sex;
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 isSex() {
if (this.sex)
return "男";
else
return "女";
}
public void setSex(String sex) {
if (sex.endsWith("男"))
this.sex = true;
else
this.sex = false;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
public class MyGsonTest {
public static void main(String[] args) {
Gson gson = new Gson();// 会转化类的所有属性
Student stu = new Student();
stu.setId(1);
stu.setName("Lily");
stu.setSex("男");
String lily = gson.toJson(stu);
System.out.println("Json字符串" + lily);
Student s = gson.fromJson(lily, Student.class);
System.out.println("Student类" + s);
System.out.println("=====================");
gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create();// 只转化类的属性中有@Expose的属性
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.setId(1);
s2.setId(2);
s3.setId(3);
s1.setName("tom");
s2.setName("john");
s3.setName("dave");
s1.setSex("男");
s2.setSex("女");
s3.setSex("男");
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
stus.add(s3);
String str2 = gson.toJson(stus);
System.out.println("list转json:" + str2);
List<Student> reStus = gson.fromJson(str2,
new TypeToken<List<Student>>() {
}.getType());
System.out.println("json转list:");
for (Student sss : reStus) {
System.out.println(sss);
}
// 输出结果如下:
// Json字符串{"id":1,"bir":"Lily","sex":true}
// Student类Student [id=1, name=Lily, sex=true]
// =====================
// list转json:[{"id":1,"bir":"tom"},{"id":2,"bir":"john"},{"id":3,"bir":"dave"}]
// json转list:
// Student [id=1, name=tom, sex=false]
// Student [id=2, name=john, sex=false]
// Student [id=3, name=dave, sex=false]
//
}
}
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
class Student {
@Expose
private int id;
@Expose
@SerializedName("bir")
private String name;
private boolean sex;
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 isSex() {
if (this.sex)
return "男";
else
return "女";
}
public void setSex(String sex) {
if (sex.endsWith("男"))
this.sex = true;
else
this.sex = false;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
public class MyGsonTest {
public static void main(String[] args) {
Gson gson = new Gson();// 会转化类的所有属性
Student stu = new Student();
stu.setId(1);
stu.setName("Lily");
stu.setSex("男");
String lily = gson.toJson(stu);
System.out.println("Json字符串" + lily);
Student s = gson.fromJson(lily, Student.class);
System.out.println("Student类" + s);
System.out.println("=====================");
gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
.create();// 只转化类的属性中有@Expose的属性
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.setId(1);
s2.setId(2);
s3.setId(3);
s1.setName("tom");
s2.setName("john");
s3.setName("dave");
s1.setSex("男");
s2.setSex("女");
s3.setSex("男");
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
stus.add(s3);
String str2 = gson.toJson(stus);
System.out.println("list转json:" + str2);
List<Student> reStus = gson.fromJson(str2,
new TypeToken<List<Student>>() {
}.getType());
System.out.println("json转list:");
for (Student sss : reStus) {
System.out.println(sss);
}
// 输出结果如下:
// Json字符串{"id":1,"bir":"Lily","sex":true}
// Student类Student [id=1, name=Lily, sex=true]
// =====================
// list转json:[{"id":1,"bir":"tom"},{"id":2,"bir":"john"},{"id":3,"bir":"dave"}]
// json转list:
// Student [id=1, name=tom, sex=false]
// Student [id=2, name=john, sex=false]
// Student [id=3, name=dave, sex=false]
//
}
}