GSON搞定任何JSON數(shù)據(jù)

Gson介紹

GSON是Google提供的用來在Java對(duì)象和JSON數(shù)據(jù)之間進(jìn)行映射的Java類庫??梢詫⒁粋€(gè)Json字符轉(zhuǎn)成一個(gè)Java對(duì)象,或者將一個(gè)Java轉(zhuǎn)化為Json字符串。
特點(diǎn):
a、快速、高效   
b、代碼量少、簡潔
c、面向?qū)ο?br> d、數(shù)據(jù)傳遞和解析

Gson的pom依賴

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

Gson的創(chuàng)建方式

方式1:Gson gson = new gson();
方式2:通過GsonBuilder(),可以配置多種配置。

Gson gson = new GsonBuilder()
                        .setLenient()// json寬松  
                        .enableComplexMapKeySerialization()//支持Map的key為復(fù)雜對(duì)象的形式  
                        .serializeNulls() //智能null  
                        .setPrettyPrinting()// 調(diào)教格式  
                        .disableHtmlEscaping() //默認(rèn)是GSON把HTML 轉(zhuǎn)義的
                        .create(); 

Gson的基本用法

之前寫過一個(gè)獲取天氣參數(shù)的API,就需要去解析返回的json數(shù)據(jù),就以此為例。
String url = "http://t.weather.sojson.com/api/weather/city/101010100";
String resultStr = HttpClientUtil.sendGetRequest(url, "UTF-8");

進(jìn)行解析

 Gson gson =new Gson();
 Map m= gson.fromJson(resultStr,Map.class);
 System.out.println(m.get("data"));

結(jié)果:

{shidu=15%, pm25=15.0, pm10=35.0, quality=優(yōu), wendu=3, ganmao=各類人群可自由活動(dòng), 
yesterday={date=06, sunrise=07:36, high=高溫 3.0℃, low=低溫 -7.0℃, sunset=17:03, 
aqi=58.0, ymd=2019-01-06, week=星期日, fx=西南風(fēng), fl=<3級(jí), type=晴, 
notice=愿你擁有比陽光明媚的心情}, forecast=[{date=07, sunrise=07:36, high=高溫 2.0℃, 
low=低溫 -7.0℃, sunset=17:04, aqi=48.0, ymd=2019-01-07, week=星期一, fx=北風(fēng), 
fl=3-4級(jí), type=多云, notice=陰晴之間,謹(jǐn)防紫外線侵?jǐn)_}, {date=08, sunrise=07:36, 
high=高溫 1.0℃, low=低溫 -9.0℃, sunset=17:05, aqi=28.0, ymd=2019-01-08, week=星期二, 
fx=北風(fēng), fl=3-4級(jí), type=晴, notice=愿你擁有比陽光明媚的心情}, {date=09, sunrise=07:36,
 high=高溫 2.0℃, low=低溫 -8.0℃, sunset=17:06, aqi=83.0, ymd=2019-01-09, week=星期三, 
fx=西南風(fēng), fl=<3級(jí), type=多云, notice=陰晴之間,謹(jǐn)防紫外線侵?jǐn)_}, {date=10, sunrise=07:36, 
high=高溫 4.0℃, low=低溫 -7.0℃, sunset=17:07, aqi=128.0, ymd=2019-01-10, week=星期四,
 fx=西南風(fēng), fl=<3級(jí), type=晴, notice=愿你擁有比陽光明媚的心情}, {date=11, sunrise=07:36, 
high=高溫 5.0℃, low=低溫 -6.0℃, sunset=17:08, aqi=238.0, ymd=2019-01-11, week=星期五,
 fx=西南風(fēng), fl=<3級(jí), type=多云, notice=陰晴之間,謹(jǐn)防紫外線侵?jǐn)_}]}
可以新建一個(gè)天氣的Bean,將返回的json數(shù)據(jù)轉(zhuǎn)換成對(duì)象
GSON直接解析成對(duì)象
ResultBean resultBean = new Gson().fromJson(resultStr,ResultBean.class);
解析簡單的json
data:{
      shidu = 15 % , 
      pm25 = 15.0,
      pm10 = 35.0, 
      quality = 優(yōu), 
      wendu = 3, 
      ganmao = 各類人群可自由活動(dòng),
     }
JsonObject jsonObject =(JsonObject) new JsonParser().parse(resultStr);
Int wendu = jsonObject.get("data").getAsJsonObject().get("wendu").getAsInt();
String quality= jsonObject.get("data").getAsJsonObject().get("quality").getAsString();
解析多層對(duì)象
  data:{
      shidu = 15 % , 
      pm25 = 15.0, 
      pm10 = 35.0, 
      quality = 優(yōu), 
      wendu = 3, 
      ganmao = 各類人群可自由活動(dòng), 
      yesterday :{
                    date = 06,
                    sunrise = 07: 36,
                    high = 高溫 3.0℃,
                    low = 低溫 - 7.0℃,
                    sunset = 17: 03,
                    aqi = 58.0,
                    ymd = 2019 - 01 - 06,
                    week = 星期日,
                    fx = 西南風(fēng),
                    fl = < 3 級(jí),
                    type = 晴,
                    notice = 愿你擁有比陽光明媚的心情
                 }
        }

 JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);
 JsonObject yesterday = jsonObject.get("data").getAsJsonObject().get("yesterday ").getAsJsonObject();
 String type  = yesterday.get("type").getAsString();
解析帶數(shù)組的json
{
shidu = 15 % , pm25 = 15.0, pm10 = 35.0, quality = 優(yōu), wendu = 3, ganmao = 各類人群可自由活動(dòng), 
yesterday = {
        date = 06,
        sunrise = 07: 36,
        high = 高溫 3.0℃,
        low = 低溫 - 7.0℃,
        sunset = 17: 03,
        aqi = 58.0,
        ymd = 2019 - 01 - 06,
        week = 星期日,
        fx = 西南風(fēng),
        fl = < 3 級(jí),
        type = 晴,
        notice = 愿你擁有比陽光明媚的心情
    }, 
forecast = [{
        date = 07,
        sunrise = 07: 36,
        high = 高溫 2.0℃,
        low = 低溫 - 7.0℃,
        sunset = 17: 04,
        aqi = 48.0,
        ymd = 2019 - 01 - 07,
        week = 星期一,
        fx = 北風(fēng),
        fl = 3 - 4 級(jí),
        type = 多云,
        notice = 陰晴之間, 謹(jǐn)防紫外線侵?jǐn)_
    }, {
        date = 08,
        sunrise = 07: 36,
        high = 高溫 1.0℃,
        low = 低溫 - 9.0℃,
        sunset = 17: 05,
        aqi = 28.0,
        ymd = 2019 - 01 - 08,
        week = 星期二,
        fx = 北風(fēng),
        fl = 3 - 4 級(jí),
        type = 晴,
        notice = 愿你擁有比陽光明媚的心情
    }, {
        date = 09,
        sunrise = 07: 36,
        high = 高溫 2.0℃,
        low = 低溫 - 8.0℃,
        sunset = 17: 06,
        aqi = 83.0,
        ymd = 2019 - 01 - 09,
        week = 星期三,
        fx = 西南風(fēng),
        fl = < 3 級(jí),
        type = 多云,
        notice = 陰晴之間, 謹(jǐn)防紫外線侵?jǐn)_
    }, {
        date = 10,
        sunrise = 07: 36,
        high = 高溫 4.0℃,
        low = 低溫 - 7.0℃,
        sunset = 17: 07,
        aqi = 128.0,
        ymd = 2019 - 01 - 10,
        week = 星期四,
        fx = 西南風(fēng),
        fl = < 3 級(jí),
        type = 晴,
        notice = 愿你擁有比陽光明媚的心情
    }, {
        date = 11,
        sunrise = 07: 36,
        high = 高溫 5.0℃,
        low = 低溫 - 6.0℃,
        sunset = 17: 08,
        aqi = 238.0,
        ymd = 2019 - 01 - 11,
        week = 星期五,
        fx = 西南風(fēng),
        fl = < 3 級(jí),
        type = 多云,
        notice = 陰晴之間, 謹(jǐn)防紫外線侵?jǐn)_
    }]
}

JsonObject jsonObject =(JsonObject) new JsonParser().parse(resultStr);
//獲取data
JsonObject data = jsonObject.get("data").getAsJsonObject();
//獲取數(shù)組
JsonArray forecast = data.getAsJsonObject().get("forecast").getAsJsonArray();
String type  = forecast.get(0).getAsJsonObject().get("type").getAsString();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容