GSON是Google提供的用來在Java對象和JSON數據之間進行映射的Java類庫。能夠將一個Json字符轉成一個Java對象,或者將一個Java轉化爲Json字符串。 特色: a、快速、高效 b、代碼量少、簡潔 c、面向對象 d、數據傳遞和解析方便json
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
複製代碼
Gson gson = new GsonBuilder()
.setLenient()// json寬鬆
.enableComplexMapKeySerialization()//支持Map的key爲複雜對象的形式
.serializeNulls() //智能null
.setPrettyPrinting()// 調教格式
.disableHtmlEscaping() //默認是GSON把HTML 轉義的
.create();
複製代碼
以前寫過一個獲取天氣參數的API,就須要去解析返回的json數據,就以此爲例。api
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=陰晴之間,謹防紫外線侵擾}]}
複製代碼
//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();
複製代碼
{
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();
複製代碼