分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
@Expose标签的2个属性.
1.1 deserialize
(boolean) 反序列化 默认 true
1.2
serialize
(boolean) 序列化 默认 true
使用 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 创建Gson对象,没有@Expose注释的属性将不会被序列化
@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"}
}
}
@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));
}