JSON conversion Tool Class (based on Google's Gson and Ali's Fastjson)

Source: Internet
Author: User

In the project we often involve the conversion of strings and various objects, so we have specially organized a common conversion method

First, the JSON conversion tool class based on the Com.google.code.gson package

1. Introduction of Gson dependencies in the Pom.xml file

        <Dependency>            <groupId>Com.google.code.gson</groupId>            <Artifactid>Gson</Artifactid>            <version>2.8.3</version>        </Dependency>

2. Writing Tool Classes Gsonutil

 PackageCom.json.util;ImportCom.google.gson.Gson;ImportCom.google.gson.reflect.TypeToken;Importjava.util.List;ImportJava.util.Map;/*** * @Description: JSON conversion Tool class based on Com.google.code.gson package *@authorLGs * @date May 30, 2018 **/ Public classGsonutil {Private StaticGson Gson =NULL; Static {        if(Gson = =NULL) {Gson=NewGson (); }    }    PrivateGsonutil () {}/*** Object turns into JSON * *@paramObject *@returnJSON*/     Public Staticstring Gsonstring (Object object) {string gsonstring=NULL; if(Gson! =NULL) {gsonstring=Gson.tojson (object); }        returngsonstring; }    /*** JSON turns into objects * *@paramgsonstring *@paramCLS *@returnObject*/     Public Static<T> T Gsontobean (String gsonstring, class<t>CLS) {T T=NULL; if(Gson! =NULL) {T=Gson.fromjson (gsonstring, CLS); }        returnT; }    /*** JSON turns into list<t> * *@paramgsonstring *@paramCLS *@returnlist<t>*/     Public Static<T> list<t> gsontolist (String gsonstring, class<t>CLS) {List<T> list =NULL; if(Gson! =NULL) {List= Gson.fromjson (gsonstring,NewTypetoken<list<t>>() {}.gettype ()); }        returnlist; }    /*** JSON is converted into list with map * *@paramgsonstring *@returnlist<map<string, t>>*/     Public Static<T> list<map<string, t>>gsontolistmaps (String gsonstring) {List<map<string, t>> list =NULL; if(Gson! =NULL) {List= Gson.fromjson (gsonstring,NewTypetoken<list<map<string, t>>>() {}.gettype ()); }        returnlist; }    /*** JSON converted to map * *@paramgsonstring *@returnmap<string, t>*/     Public Static<T> map<string, t>gsontomaps (String gsonstring) {Map<string, t> map =NULL; if(Gson! =NULL) {Map= Gson.fromjson (gsonstring,NewTypetoken<map<string, t>>() {}.gettype ()); }        returnmap; }}
Ii. The JSON conversion tool class based on Ali's Fastjson package

1. Introduction of Fastjson dependencies in the Pom.xml file

        <Dependency>            <groupId>Com.alibaba</groupId>            <Artifactid>Fastjson</Artifactid>            <version>1.2.4</version>        </Dependency>

2. Writing Tool Classes Fastjsonutils

 PackageCom.json.util;Importjava.util.List;ImportJava.util.Map;ImportCom.alibaba.fastjson.JSON;Importcom.alibaba.fastjson.TypeReference;/*** * @Description: JSON conversion Tool class based on Fastjson package *@authorLGs * @date May 30, 2018 **/ Public classFastjsonutils {/*** Function Description: Convert JSON data to a specified Java object *@paramjsondata JSON data *@paramClazz The specified Java object *@returnThe specified Java object*/     Public Static<T> T Getjsontobean (String jsondata, class<t>clazz) {        returnJson.parseobject (Jsondata, clazz); }    /*** Function Description: Convert Java object to JSON data *@paramObject Java Object *@returnJSON data*/     Public StaticString Getbeantojson (Object object) {returnjson.tojsonstring (object); }    /*** Function Description: Convert JSON data to the specified list of Java objects *@paramjsondata JSON data *@paramClazz The specified Java object *@returnlist<t>*/     Public Static<T> list<t> getjsontolist (String jsondata, class<t>clazz) {        returnJson.parsearray (Jsondata, clazz); }    /*** Function Description: Convert JSON data into more complex list<map<string, object>> *@paramjsondata JSON data *@returnlist<map<string, object>>*/     Public StaticList<map<string, object>>Getjsontolistmap (String jsondata) {returnJson.parseobject (Jsondata,NewTypereference<list<map<string, object>>>() {        }); }}

JSON conversion Tool Class (based on Google's Gson and Ali's Fastjson)

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.