天天看點

gson生成jsonobject_GSON搞定JSON資料

GSON是Google提供的用來在Java對象和JSON資料之間進行映射的Java類庫。可以将一個Json字元轉成一個Java對象,或者将一個Java轉化為Json字元串。

特點:

a、快速、高效

b、代碼量少、簡潔

c、面向對象

d、資料傳遞和解析友善

Gon介紹

com.google.code.gson

gson

2.8.0

Gson的建立方式

Gson gson = new gson();

通過GsonBuilder(),可以配置多種配置。

Gson gson = new GsonBuilder()

.setLenient()// json寬松

.enableComplexMapKeySerialization()//支援Map的key為複雜對象的形式

.serializeNulls() //智能null

.setPrettyPrinting()// 調教格式

.disableHtmlEscaping() //預設是GSON把HTML 轉義的

.create();

Gson的基本用法

之前寫過一個擷取天氣參數的API,就需要去解析傳回的json資料,就以此為例。

擷取天氣參數

String url = "http://t.weather.sojson.com/api/weather/city/101010100";

String resultStr = HttpClientUtil.sendGetRequest(url, "UTF-8");

傳回結果(就不格式化了)

{shidu=15%, pm25=15.0, pm10=35.0, quality=優, wendu=3, ganmao=各類人群可自由活動,

yesterday={date=06, sunrise=07:36, high=高溫 3.0℃, low=低溫 -7.0℃, sunset=17:03,

aqi=58.0, ymd=2019-01-06, week=星期日, fx=西南風, fl=<3級, 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=北風,

fl=3-4級, type=多雲, notice=陰晴之間,謹防紫外線侵擾}, {date=08, sunrise=07:36,

high=高溫 1.0℃, low=低溫 -9.0℃, sunset=17:05, aqi=28.0, ymd=2019-01-08, week=星期二,

fx=北風, fl=3-4級, 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=西南風, fl=<3級, type=多雲, notice=陰晴之間,謹防紫外線侵擾}, {date=10, sunrise=07:36,

high=高溫 4.0℃, low=低溫 -7.0℃, sunset=17:07, aqi=128.0, ymd=2019-01-10, week=星期四,

fx=西南風, fl=<3級, 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=西南風, fl=<3級, type=多雲, notice=陰晴之間,謹防紫外線侵擾}]}

可以建立一個天氣的Bean,将傳回的json資料轉換成對象

//GSON直接解析成對象

ResultBean resultBean = new Gson().fromJson(resultStr,ResultBean.class);

解析簡單的json

data:{

shidu = 15 % ,

pm25 = 15.0,

pm10 = 35.0,

quality = 優,

wendu = 3,

ganmao = 各類人群可自由活動,

}

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();

解析多層對象

data:{

shidu = 15 % ,

pm25 = 15.0,

pm10 = 35.0,

quality = 優,

wendu = 3,

ganmao = 各類人群可自由活動,

yesterday :{

date = 06,

sunrise = 07: 36,

high = 高溫 3.0℃,

low = 低溫 - 7.0℃,

sunset = 17: 03,

aqi = 58.0,

ymd = 2019 - 01 - 06,

week = 星期日,

fx = 西南風,

fl = < 3 級,

type = 晴,

notice = 願你擁有比陽光明媚的心情

}

}

JsonObject jsonObject = (JsonObject) new JsonParser().parse(resultStr);

JsonObject yesterday = jsonObject.get("data").getAsJsonObject().get("yesterday ").getAsJsonObject();

String type = yesterday.get("type").getAsString();

解析帶數組的json

{

shidu = 15 % , pm25 = 15.0, pm10 = 35.0, quality = 優, wendu = 3, ganmao = 各類人群可自由活動,

yesterday = {

date = 06,

sunrise = 07: 36,

high = 高溫 3.0℃,

low = 低溫 - 7.0℃,

sunset = 17: 03,

aqi = 58.0,

ymd = 2019 - 01 - 06,

week = 星期日,

fx = 西南風,

fl = < 3 級,

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 = 北風,

fl = 3 - 4 級,

type = 多雲,

notice = 陰晴之間, 謹防紫外線侵擾

}, {

date = 08,

sunrise = 07: 36,

high = 高溫 1.0℃,

low = 低溫 - 9.0℃,

sunset = 17: 05,

aqi = 28.0,

ymd = 2019 - 01 - 08,

week = 星期二,

fx = 北風,

fl = 3 - 4 級,

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 = 西南風,

fl = < 3 級,

type = 多雲,

notice = 陰晴之間, 謹防紫外線侵擾

}, {

date = 10,

sunrise = 07: 36,

high = 高溫 4.0℃,

low = 低溫 - 7.0℃,

sunset = 17: 07,

aqi = 128.0,

ymd = 2019 - 01 - 10,

week = 星期四,

fx = 西南風,

fl = < 3 級,

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 = 西南風,

fl = < 3 級,

type = 多雲,

notice = 陰晴之間, 謹防紫外線侵擾

}]

}

JsonObject jsonObject =(JsonObject) new JsonParser().parse(resultStr);

//擷取data

JsonObject data = jsonObject.get("data").getAsJsonObject();

//擷取數組

JsonArray forecast = data.getAsJsonObject().get("forecast").getAsJsonArray();

String type = forecast.get(0).getAsJsonObject().get("type").getAsString();