JSON parsing class library Gson (2)---Generic object map, list, and JSON string interchange

Source: Internet
Author: User
Tags serialization

JSON parsing class library Gson (2)---Generic object map, list, and JSON string interchange


---Gson class library to learn, generate and parse JSON data, and turn JSON strings to and from Java objects


First, the preface
This article extends the previous article and continues to introduce basic gson usage. In this article, we will describe how to implement Java generic objects such as map<>,list<> and JSON strings.
second, the basic use of Gson

serialization and deserialization of generic collection-type objects --------------------------------------------------------------------------------- ------------------ situation 1 "non-generic": Serialization and deserialization of Java arrays Unlike generics, arrays are not generics.
java Array = = = "JSON string JSON string = =" Java array

       
       
        
        GsonGson =NewGsonbuilder ()//. Setpretty Printing ()//Format output (serialization). Setdateformat ("Yyyy-mm-dd HH:mm:ss"
        
        

        
        
        
        
         
         //Serialization date formatted output. Create (); User user1 =NewUser ("1", "Wang Chongyang",NewDate ()); User user2 =NewUser ("2", "Guo Jing",NewDate ()); User User3 =NewUser ("3", "Huang Rong",NewDate ()); User[] Usersarray = {user1, user2, user3};
        
        
        
        
         
          Array Object/**
        
        
        
        
         
         * Serialized Array * *
        
        
        
        
         
         String jsonarrstring = Gson.tojson (Usersarray); System. out. println ("Array serialization  =="   " + jsonarrstring");
        
        
        
        
         
         /**  *  Array serialization  == " [   {     "I
        
        
        
        
         
         D ": " 1 ",     " name ": " Wang Chongyang ",
        
        
        
        
         
              "Birthday":  "2017-05-03 16:06:50"   }, &nbsp
        
        
        
        
         
         ;  {     "id":  "2",
        
        
        
        
         
              "name":  "Guo Jing",      "Birthday":  "2017-05-03 16:06:50" &NBSP;&NBSP;},
        
        
        
        
         
           {      "id":  "3",      "NA" Me ": " Huang Rong ",     " Birthday ": " 2017-05-0
        
        
        
        
         
         3 16:06:50 "&NBSP;&NBSP;}
        
        

        
        
         
         ]  */ /** a  * json string to deserialize an array of objects, the type parameters of which the arrays are deserialized can be straight
         
          Class that is connected to an array, asUser[].class* * user[] Usersarr = Gson.fromjson (jsonarrstring, user[).class); for (int i = 0; i < usersarr.length; i++) {Useru1 = Usersarr[i]; System. out. println ("JSON string deserializes an array of objects = =" + u1); * * User Object = = "User [id=1, name= Wang Chong, birthday=wed May 03 16:1 4:14 CST 2017] User Object = = "User [id=2, name= Guo Jing, birthday=wed May 16:14:14 CST 2017] User Object = = "User [id=3, name= Huang Rong, bi
        
        
        
        Rthday=wed may 16:14:14 CST 2017] */ }
Attention: The byte-code file of an array can reflect the object type in a specific array, but list<user> cannot be used directly with List<user>.class or List.class. Because generics compile to bytecode are erase type,list<user> and list<dept> are completely the same after being translated into bytecode, no difference.
The generic collection has to use the new TypeToken (list<user>) {}.gettype () as the second parameter of the inverse sequence.

Case 2:java Map serialization and deserialization
Entity classes:

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.