<span id="Label3"></p><p><p>Recently I was working on a Java Web service project and needed to use jason, who was not particularly proficient in java, and began to search the JSON class libraries of some Java Platforms.</p></p><p><p>Found Google's gson, because before protocolbuf some understanding, with some curiosity, I began to use the Gson.</p></p><p><p>by comparison, Gson and other existing Java JSON class libraries are most different when Gson need to serialize an entity class without using annotation to identify the fields that need to be serialized, while Gson can flexibly configure the fields that need to be serialized by using Annotation.</p></p><p><p>The following is a simple example:</p></p><pre><span style="color: #000000;">person <span style="color: #000000;">Entity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;">person <span style="color: #000000;">{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String name; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;">age <span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @return the name</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getName () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">name; } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * @param name the name to set</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setName (String Name) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Name =<span style="color: #000000;"><span style="color: #000000;">name; } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">* <span style="color: #008000;">* @return</span> the Age</span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">getage () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">age <span style="color: #000000;">; } </span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;"><span style="color: #008000;">* * @param</span> age-the age-to Set</span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Setage (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;">age <span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Age =<span style="color: #000000;">age <span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String toString () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Name +<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">:</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>+<span style="color: #000000;">age <span style="color: #000000;">; }}</span></span></pre><p><p>Entities are simple, two fields, and of course the fields in an entity can be list or set Type.</p></p><pre><pre> <span style="color: #000000;"> Generate JSON string Gson gson </span> = <span style="color: #0000ff;">new </span> <span style="color: #000000;"> Gson (); List </span> <Person> persons = <span style="color: #0000ff;">new </span> arraylist<person><span style=" Color: #000000; ">();</span> <span style="color: #0000ff;">for </span> (<span style="color: #0000ff;">int </span> i = <span style="color: #800080;">0 </span>; i < <span style="color: # 800080; ">10 </span>; I++<span style="color: #000000;" ) {person p < span> = <span style="color: #0000ff;">new </span> <span style="color: #0 00000; "> person (); P.setname (</span> <span style="color: #800000;" "< span> <span style=" color: #800000; ">name </span> <span style=" Color: #800000; "> "</span> +<span style=" color: #000000; "> i); P.setage (i </span> * <span style="color: #800080;">5 </span> <span style="color: #000000;">); Persons.add (p);} String str </span> = Gson.tojson (persons); </span></span></pre></pre><p><p>The above code focuses on the Gson object, which provides the Tojason () method to convert the object to a JSON string, the value of the Str object of the above code is:</p></p><p><p>[{' name ': ' NAME0 ', ' age ': 0},{' name ': ' name1 ', ' age ': 5},{' name ': ' name2 ', ' age ': 10},{' name ': ' Name3 ', ' age ': 15},{' name ' : ' Name4 ', ' age ': 20},{' name ': ' name5 ', ' age ': 25},{' name ': ' name6 ', ' age ': 30},{' name ': ' Name7 ', ' age ': 35},{' name ': ' Name8 "," Age ": 40},{" name ":" name9 "," age ": 45}]</p></p><p><p>Very standard JSON data, very simple, Hehe.</p></p><p><p>The following is a look at the deserialization of gson, Gson provides the Fromjson () method for implementing a method from a Json-related object to a Java Entity.</p></p><p><p>In everyday applications, we typically encounter two situations, turning into a single entity object and converting it into a list of objects or other Structures.</p></p><p><p>Let's look at the first type:</p></p><p><p>For example, the JSON string is: [{"name": "name0", "age": 0}]</p></p><p><p>Code:</p></p><pre><pre>Person person = Gson.fromjson (str, Person. <span style="color: #0000ff;"> Class</span>);</pre></pre><p><p>Provides two parameters, the JSON string, and the type of object that needs to be converted.</p></p><p><p>second, Convert to List type:</p></p><p><p>Code:</p></p><pre><pre><span style="color: #0000ff;">New</span> typetoken<list<person>><span style="color: #000000;">() {}.gettype ());</span> <span style="color: #0000ff;"></span> for (<span style="color: #0000ff;">int</span><span style="color: #800080;">0</span>; i < ps.size (); i++<span style="color: #000000;">) { </span>= ps.<span style="color: #0000ff;">get</span><span style="color: #000000;">(i); System.</span> <span style="color: #0000ff;"></span> out <span style="color: #000000;">. println (p.tostring ());}</span></pre></pre><p><p>You can see that the code above uses typetoken, which is a data type converter provided by gson, and can support a variety of data collection type Conversions.</p></p><p><p>Gson basic use is so much, as to annotation aspects can refer to Gson official documents, hope to beginner Java and Gson classmates Help.</p></p><p><p>Android Gson Using Notes</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service