JSON operation of Java (Gson)

Source: Internet
Author: User
Tags tojson

Use the Gson package to manipulate JSON data.

Dependency Package: Gson-2.8.2.jar

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>

The code is as follows:

1  PackageJson.gson;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 ImportJava.util.Map;6 7 ImportCom.google.gson.Gson;8 ImportCom.google.gson.JsonArray;9 Importcom.google.gson.JsonElement;Ten ImportCom.google.gson.JsonParser; One ImportCom.google.gson.reflect.TypeToken; A  - /** - * Use Gson to manipulate JSON the * 1. Object entity class, LIST,MAP, etc. converted into----->jsonstring (for data transfer) - * 2.jsonString converted to-----> object entity class, List,map (parse the returned data) - * 3. I'm using the Gson-2.2.2.jar. -  * +  */ -  Public classJsonutil { +  A  at     Private StaticGson Gson =NULL; -  -     Static { -         if(Gson = =NULL) { -Gson =NewGson (); -         } in     } -  to  +     /** - * Turn Object object into JSON string (jsonstring) the * Boject can be a list or a bean (object type) *      * $      * @paramObjectPanax Notoginseng      */ -      Public StaticString gsonstring (Object object) { theString gsonstring =NULL; +         if(Gson! =NULL) { AGsonstring =Gson.tojson (object); the         } +         returngsonstring; -     } $  $  -     /** - * The import parameter is a JSON object the * Turn gsonobjectstring into a generic bean (entity Class) -      *Wuyi      * @paramgsonstring the      */ -      Public Static<T> T Gsontobean (String gsonstring, class<t>CLS) { WuT t =NULL; -         if(Gson! =NULL) { Aboutt =Gson.fromjson (gsonstring, CLS); $         } -         returnT; -     } -  A     /** + * The entry parameter here is a string of JSON array type the * jsonarraystring turn into List -      */ $      Public Static<T> list<t> jsonarraytolist (String json, class<t>CLS) { theGson Gson =NewGson (); thelist<t> list =NewArraylist<t>(); theJsonarray array =NewJsonparser (). Parse (JSON). Getasjsonarray (); the          for(Finaljsonelement Elem:array) { - List.add (Gson.fromjson (Elem, CLS)); in         } the         returnlist; the     } About  the  the     /** the * List is not required for generics + * jsonstring turns into list with map -      */ the      Public Static<T> list<map<string, t>>Gsontolistmap (String gsonstring) {BayiList List =NULL; the         if(Gson! =NULL) { theList =Gson.fromjson (gsonstring, -                     NewTypetoken<list>() { - }.gettype ()); the         } the         returnlist; the     } the  -     /** the * Request list is all map the * jsonstring turns into list with map the      */94      Public Static<T> list<map<string, t>>gsontolistmaps (String gsonstring) { thelist<map<string, t>> list =NULL; the         if(Gson! =NULL) { theList =Gson.fromjson (gsonstring,98                     NewTypetoken<list<map<string, t>>>() { About }.gettype ()); -         }101         returnlist;102     }103 104  the     /**106 * jsonstring turns into map.107      */108      Public Static<T> map<string, t>gsontomaps (String gsonstring) {109map<string, t> map =NULL; the         if(Gson! =NULL) {111Map = Gson.fromjson (gsonstring,NewTypetoken<map<string, t>>() { the }.gettype ());113         } the         returnmap; the     } the 117     /**118 * Map to Jsonstring, specifying the map type119      */ -      Public StaticString Maptojson (map<string, object>map) {121String str =NULL;122         if(Gson! =NULL) {123str =Gson.tojson (map);124         } the         returnstr;126     }127  -}

Test code:

1  PackageJson.gson;2 3 ImportBean. User;4 5  Public classTestgson {6 7     Private StaticUser User =NULL;8 9     Static {Tenuser =NewUser ("Tom", "the most street"); One     } A  -      Public Static voidMain (string[] args) { -String Jsonstr =jsonutil.gsonstring (user); the System.out.println (JSONSTR); -  -String jsonStr2 = "{\" name\ ": \" tom\ ", \" age\ ": 25,\" address\ ": \" Street\ "}"; -User User2 = Jsonutil.gsontobean (jsonStr2, user.class); + System.out.println (user2); -     } +}

Operation Result:

{"Name": "Tom", "Age": "Address": "The Street"}
User[name= ' Tom ', age=25, address= ' the street ']

JSON operation of Java (Gson)

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.