标签:view details 分享 [] 人工智能 art bool oid json
1.1 deserialize
(boolean)
反序列化 默认 trueserialize
(boolean) 序列化 默认 true
@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"}
}
}
2.使用@SerializedName标签定义属性序列化后的名字
@Expose
@SerializedName("name")
private String username;
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();
//{"name":"lemon","age":27,"list":["l1","l2"]}
System.out.println(g1.toJson(user));
//使用 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Gson g2 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
//{"name":"lemon"}
System.out.println(g2.toJson(user));
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
gson的 Expose注解和 SerializedName注解
标签:view details 分享 [] 人工智能 art bool oid json
原文地址:https://www.cnblogs.com/skinchqqhah/p/10350607.html