gson的@Expose注解和@SerializedName注解
来源:互联网 发布:双乾网络支付有限公司 编辑:程序博客网 时间:2024/06/04 23:24
1.使用@Expose可以区分实体中不想被序列化的属性
@Expose标签的2个属性.
serialize
- public
classUser { -
- @Expose
- private
username;String - @Expose(serialize=false)
- private
int age; -
- private
list;List -
- public
intUser(String username, age) { - super();
- this.username
= username; - this.age
= age; - }
-
- public
String getUsername() { - return
username; - }
- public
voidsetUsername(String username) { - this.username
= username; - }
- public
intgetAge() { - return
age; - }
- public
void intsetAge( age) { - this.age
= age; - }
- public
List getList() { - return
list; - }
- public
voidsetList(List list) { - this.list
= list; - }
-
- public
static voidmain(String []args){ - User
newuser = "lemon",27);User( - List
newlist = ArrayList(); - list.add("l1");
- list.add("l2");
- user.setList(list);
- Gson
newg1 = Gson(); - //使用
Gson();new - System.out.println(g1.toJson(user));
//{"username":"lemon","age":27,"list":["l1","l2"]} - //使用
new GsonBuilder().excludeFieldsWithoutExpo seAnnotation().create(); - Gson
newg2 = GsonBuilder().excludeFieldsWithoutExpo seAnnotation().create(); - System.out.println(g2.toJson(user));
//{"username":"lemon"} -
} - }
@Expose
@SerializedName("name")
private
username; String
public
static void main(String []args){ User
new user = "lemon",27); User( List
new list = ArrayList(); list.add("l1");
list.add("l2");
user.setList(list);
Gson
new g1 = Gson(); //使用
Gson(); new //{"name":"lemon","age":27,"list":["l1","l2"]}
System.out.println(g1.toJson(user));
//使用
new GsonBuilder().excludeFieldsWithoutExpo seAnnotation().create(); Gson
new g2 = GsonBuilder().excludeFieldsWithoutExpo seAnnotation().create(); //{"name":"lemon"}
System.out.println(g2.toJson(user));
}
补充:简单方式:排除部分不序列化的属性(特别是循环引用的)
可以使用关键字: transient。
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- gson的@Expose注解和@SerializedName注解
- Gson的@SerializedName和@Expose注解
- google的@SerializedName和@Expose注解
- google的@SerializedName和@Expose注解
- 通透Gson@Expose注解、@SerializedName、解析json数据
- Android Gson @SerializedName注解
- @serializedname注解的意思
- Gson教程七(译):@SerializedName和@Expose
- Java NIO: Non-blocking Server
- 使用Emacs ediff作为git diff工具
- python(十二)下:ORM框架SQLAlchemy使用学习
- 关于ToolBar和Actionbar左上角返回箭头的问题
- 多态特性笔记
- gson的@Expose注解和@SerializedName注解
- python科学运算库numpy,pandas学习记录
- ServletConfig对象用法
- 词法分析程序之正规式转换成NFA
- Unity解决脚本相互调用的几种方法
- 《Photoshop智能手机APP界面设计》学习笔记
- 连接游戏(poj 2084)
- web.xml文件初始化过程
- STL常用容器浅谈