@Expose
private String username;
@Expose(serialize=false)
private int age ;
private List<String> list;
public User(String username, int age) {
super();
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public static void main(String []args){
User user = new User("lemon",27);
List<String> list = new ArrayList<String>();
list.add("l1");
list.add("l2");
user.setList(list);
Gson g1 = new Gson();
//使用 new Gson();
System.out.println(g1.toJson(user)); //{"username":"lemon","age":27,"list":["l1","l2"]}
//使用 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Gson g2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
System.out.println(g2.toJson(user)); //{"username":"lemon"}