`
thecloud
  • 浏览: 880778 次
文章分类
社区版块
存档分类
最新评论

gson的@Expose注解和@SerializedName注解

 
阅读更多

gson的@Expose注解和@SerializedName注解<wbr></wbr>


@Expose标签的2个属性.<wbr></wbr>

<wbr> 1.1<a href="http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/annotations/Expose.html#deserialize()"><wbr>deserialize</wbr></a></wbr>
<wbr>(boolean) 反序列化 默认 true</wbr>
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>1.2<wbr></wbr>serialize<wbr> (boolean) 序列化 默认 true</wbr>

<wbr><wbr><wbr> 使用</wbr></wbr></wbr><wbr>new</wbr><wbr>GsonBuilder().excludeFieldsWithoutExpo<wbr>seAnnotation().create();</wbr></wbr>创建Gson对象,没有@Expose注释的属性将不会被序列化

<wbr><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">class</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>User<wbr>{</wbr></wbr></span><wbr><blockquote style="line-height:25px; margin:auto 0px auto 35px; color:rgb(51,51,51); font-size:1em"> <div style="line-height:25px"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#646464; line-height:25px; font-size:10.5pt">@Expose</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">private</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>String<wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">username</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#646464; line-height:25px; font-size:10.5pt">@Expose</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">(serialize=</span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">false</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">)</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">private</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">int</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">age</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>;</wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <wbr></wbr></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">private</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>List&lt;String&gt;<wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">list</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <wbr></wbr></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>User(String<wbr>username,<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">int</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>age)<wbr>{</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">super</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">();</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">this</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">username</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>=<wbr>username;</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">this</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">age</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>=<wbr>age;</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <wbr></wbr></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>String<wbr>getUsername()<wbr>{</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">return</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">username</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">void</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>setUsername(String<wbr>username)<wbr>{</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">this</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">username</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>=<wbr>username;</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">int</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>getAge()<wbr>{</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">return</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">age</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">void</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>setAge(</wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">int</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>age)<wbr>{</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">this</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">age</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>=<wbr>age;</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>List&lt;String&gt;<wbr>getList()<wbr>{</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">return</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">list</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">void</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>setList(List&lt;String&gt;<wbr>list)<wbr>{</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">this</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">list</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>=<wbr>list;</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <wbr></wbr></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">static</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">void</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>main(String<wbr>[]args){</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">User<wbr>user<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>User(</wbr></span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"lemon"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">,27);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">List&lt;String&gt;<wbr>list<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>ArrayList&lt;String&gt;();</wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">list.add(</span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"l1"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">list.add(</span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"l2"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">user.setList(list);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">Gson<wbr>g1<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>Gson();</wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//使用<wbr>new<wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt; text-decoration:underline">Gson</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">();</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">System.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-style:italic; font-size:10.5pt">out</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.println(g1.toJson(user));<wbr></wbr></span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//{"</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt; text-decoration:underline">username</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">":"lemon","age":27,"list":["l1","l2"]}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//使用<wbr>new<wbr>GsonBuilder().excludeFieldsWithoutExpo<wbr>seAnnotation().create();</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">Gson<wbr>g2<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>GsonBuilder().excludeFieldsWithoutExpo<wbr>seAnnotation().create();</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">System.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-style:italic; font-size:10.5pt">out</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.println(g2.toJson(user));<wbr></wbr></span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//{"</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt; text-decoration:underline">username</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">":"lemon"}</span></p> </div> </blockquote> <div style="line-height:25px; color:rgb(51,51,51)"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr><wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><br style="line-height:25px"></span></p> </div> <div style="line-height:25px; color:rgb(51,51,51)"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></p> </div> <p style="padding-bottom:0px; line-height:25px; margin:0px 0px 10px; padding-left:0px; padding-right:0px; color:rgb(51,51,51); padding-top:0px"> <br style="line-height:25px"></p> <span style="color:#333333">2.使用@</span><span style="color:#333333; line-height:25px"><span style="line-height:25px">SerializedName</span><span style="line-height:25px">标签定义属性序列化</span></span><span style="color:#333333">后的名字</span><br style="line-height:25px"><br style="line-height:25px"><blockquote style="line-height:25px; margin:auto 0px auto 35px; color:rgb(51,51,51); font-size:1em"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#646464; line-height:25px; font-size:10.5pt">@Expose</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#646464; line-height:25px; font-size:10.5pt">@SerializedName</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">(</span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"name"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">)</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">private</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>String<wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-size:10.5pt">username</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">;</span><br style="line-height:25px"></p> </blockquote> <blockquote style="line-height:25px; margin:auto 0px auto 35px; font-size:1em"> <div style="line-height:25px"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; color:rgb(51,51,51); padding-top:0px"> <span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">public</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">static</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">void</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>main(String<wbr>[]args){</wbr></wbr></span></p> <blockquote style="line-height:25px; margin:auto 0px auto 35px; color:rgb(51,51,51); font-size:1em"> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">User<wbr>user<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>User(</wbr></span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"lemon"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">,27);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">List&lt;String&gt;<wbr>list<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>ArrayList&lt;String&gt;();</wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">list.add(</span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"l1"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">list.add(</span><span style="font-family:'Courier new'; color:#2a0ff; line-height:25px; font-size:10.5pt">"l2"</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">user.setList(list);</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">Gson<wbr>g1<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>Gson();</wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//使用<wbr>new<wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt; text-decoration:underline">Gson</span><span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">();</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; line-height:25px; font-size:10.5pt">//{"name":"lemon","age":27,"list":["l1","l2"]}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">System.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-style:italic; font-size:10.5pt">out</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.println(g1.toJson(user));<wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//使用<wbr>new<wbr>GsonBuilder().excludeFieldsWithoutExpo<wbr>seAnnotation().create();</wbr></wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">Gson<wbr>g2<wbr>=<wbr></wbr></wbr></wbr></span><span style="font-family:'Courier new'; color:#7f055; line-height:25px; font-size:10.5pt; font-weight:bold">new</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><wbr>GsonBuilder().excludeFieldsWithoutExpo<wbr>seAnnotation().create();</wbr></wbr></span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#3f7f5f; line-height:25px; font-size:10.5pt">//{"name":"lemon"}</span></p> <p style="padding-bottom:0px; line-height:25px; margin:0pt 0px; padding-left:0px; padding-right:0px; padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">System.</span><span style="font-family:'Courier new'; color:#00c0; line-height:25px; font-style:italic; font-size:10.5pt">out</span><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">.println(g2.toJson(user));<wbr></wbr></span></p> </blockquote> <p style="padding-bottom:0px; line-height:25px; text-indent:-63pt; margin:0pt 0px 0pt 63pt; padding-left:0px; padding-right:0px; color:rgb(51,51,51); padding-top:0px"> <span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt"><span style="font-family:'Courier new'; color:#000000; line-height:25px; font-size:10.5pt">}</span></span></p> </div> </blockquote> </wbr></wbr>
分享到:
评论

相关推荐

    android studio gson插件自动生成实体类

    android studio gson插件自动生成实体类。我的博客地址:http://blog.csdn.net/qq_16064871

    gson 使用Gson将Java对象转换为JSON

    Gson里最重要的对象有2个Gson 和 GsonBuilder Gson有2个最基本的方法 1) toJson() – 转换java 对象到JSON 2) fromJson() – 转换JSON到java对象 下面是几个小例子 1. toJson() example Java 代码 收藏代码...

    googleGson JsonObject json转换包

    com.google.gson.annotations.SerializedName.class com.google.gson.annotations.Since.class com.google.gson.annotations.Until.class com.google.gson.internal.ConstructorConstructor.class ...

    GSON JAR包 最新包和历史包 gson-2.10.1

    介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...

    Gson解析(List和Map)格式json数据 - CSDN博客1

    Gson解析(List和Map)格式json数据 - CSDN博客原创2013年11月23日 21:54:46Gson解析(List和Map)格式json数据天

    MySQL驱动和gson包

    MySQL驱动和gson包MySQL驱动和gson包MySQL驱动和gson包

    om.google.gson.Gson Gson maven依赖

    om.google.code.gson/gson/2.6.2 Gson maven依赖 java 开发包

    gson-2.8.5版本的jar包

    Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个JSON字符串转成一个Java对象(反序列化),或者反过来(序列化)。 GSON地址:google/gson (github.com) Android引入GSON: ...

    Android注解框架AndroidInject.zip

    AndroidInject 是 Android 注解框架,以简化 Android 开发 目前完成的注解(持续增加中): @AINoTitle: 类注解,... 使用网络请求的注解,需要gson.jar的支持 使用文件上传的注解,需要httpmime.jar的支持

    gson-2.8.0-API文档-中文版.zip

    赠送jar包:gson-2.8.0.jar; 赠送原API文档:gson-2.8.0-javadoc.jar; 赠送源代码:gson-2.8.0-sources.jar; 赠送Maven依赖信息文件:...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    gson jar包下载

    gson jar包 gson jar包下载 gson jar包下载 gson jar包下载

    com.google.gson.Gson 2.8.1 2.8.2 jar包 gson

    com.google.gson.Gson 2.8.1 2.8.2 jar包 gson,直接解压到maven仓库,或者提取里面的jar包都可以使用

    com.google.gson.Gson.jar

    json我们在网络请求中经常用到,最近最火的微信小程序开发中... 就会报错,因为对于嵌套类型的json它是解释不出来的,这时就需要用到com.google.gson.Gson.jar了。资源中提供了jar包与代码示例,一句代码解决您的问题。

    gson-2.8.7-API文档-中文版.zip

    赠送jar包:gson-2.8.7.jar; 赠送原API文档:gson-2.8.7-javadoc.jar; 赠送源代码:gson-2.8.7-sources.jar; 赠送Maven依赖信息文件:...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    auto-value-gson:AutoValue Extension添加Gson DeSerializer支持

    AutoValue:Gson扩展 Google的扩展,可为每个带有AutoValue注释的对象创建一个简单的 TypeAdapterFactory。 用法 只需在您的项目中包括auto-value-gson,然后将一个公共静态方法添加到您的@AutoValue注释类中即可...

    gson2.8.0和2.8.6.rar

    gson jar ,2.8.0 和2.8.6 ,2.8.0是使用java1.8编译,gson2.8.6包含源码,使用java11编译,需要依赖java11才能使用

    gson-2.8.9-API文档-中文版.zip

    赠送jar包:gson-2.8.9.jar; 赠送原API文档:gson-2.8.9-javadoc.jar; 赠送源代码:gson-2.8.9-sources.jar; 赠送Maven依赖信息文件:...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    com.google.gson.Gson 2.8.5 jar包

    截止至2018-11-27,github上com.google.gson.Gson 最新的2.8.5jar包。强大的json字符串解析功能及将字符串转换为json格式。我16年上传的免费下载的2.8.0版本不知道为什么变成下载需要50分了

    Gson解析json数据

    这里进行介绍Gson解析复杂Json和简单Json的数据方式

    Gson-2.8.1的官方jar包合集【gson-2.8.1.jar,gson-2.8.1-sources.jar】

    Gson-2.8.1的官方jar包合集【gson-2.8.1.jar,gson-2.8.1-sources.jar】, 包含Gson核心包和源码包。

Global site tag (gtag.js) - Google Analytics