前言:
在日常開發(fā)中筐喳,我們可能會面對很多復(fù)雜的Json數(shù)據(jù),每次都要費點腦筋去寫解析Json的代碼箕速。Json解析的方法有很多種涧狮,今天炕矮,我們來介紹如何用Gson+GsonFormat來解析Json吧。
一勋篓、Gson與GsonFormat的導入
Gson是Google提供的用來在Java對象和JSON數(shù)據(jù)之間進行映射的Java類庫吧享。可以將一個Json字符轉(zhuǎn)成一個Java對象譬嚣,或者將一個Java轉(zhuǎn)化為Json字符串钢颂。
到(Gson的GitHub)里可以看到,(在有網(wǎng)絡(luò)的情況下)我們只需將下面這行代碼放進build.gradle的dependencies下就行拜银。
compile 'com.google.code.gson:gson:2.8.4'
Gson的導入:
接下來是GsonFormat殊鞭,GsonFormat可以幫你格式化Json數(shù)據(jù),并且自動生成相應(yīng)的屬性類尼桶。
Gson的導入我們到Settings/plugins/Browse repositories里導入就行了操灿。(Mac系統(tǒng)的plugins在左上角的Android Studio/Preferences目錄下)
安裝完成后記得點restart Android Studio重啟。
GsonFormat的導入:
如何驗證安裝是否成功呢泵督?在java文件上按alt+S(蘋果端按command+N再點擊GsonFormat)趾盐,彈出GsonFormat的窗口則代表安裝成功。
二、GsonFormat和Gson的使用
首先是GsonFormat救鲤,這次我們來解析的json數(shù)據(jù)如下:
{"date":"20180507","message":"Success !","status":200,"city":"廣州","count":1642,"data":{"shidu":"86%","pm25":14.0,"pm10":24.0,"quality":"優(yōu)","wendu":"26","ganmao":"各類人群可自由活動","yesterday":{"date":"06日星期日","sunrise":"05:52","high":"高溫 30.0℃","low":"低溫 24.0℃","sunset":"18:56","aqi":44.0,"fx":"無持續(xù)風向","fl":"<3級","type":"雷陣雨","notice":"帶好雨具久窟,別在樹下躲雨"},"forecast":[{"date":"07日星期一","sunrise":"05:51","high":"高溫 27.0℃","low":"低溫 21.0℃","sunset":"18:57","aqi":43.0,"fx":"無持續(xù)風向","fl":"<3級","type":"大到暴雨","notice":"雨勢轉(zhuǎn)大,在外找好避雨處"},{"date":"08日星期二","sunrise":"05:51","high":"高溫 26.0℃","low":"低溫 21.0℃","sunset":"18:57","aqi":49.0,"fx":"無持續(xù)風向","fl":"<3級","type":"大到暴雨","notice":"雨勢轉(zhuǎn)大本缠,在外找好避雨處"},{"date":"09日星期三","sunrise":"05:50","high":"高溫 25.0℃","low":"低溫 21.0℃","sunset":"18:58","aqi":57.0,"fx":"無持續(xù)風向","fl":"<3級","type":"中雨","notice":"記得隨身攜帶雨傘哦"},{"date":"10日星期四","sunrise":"05:49","high":"高溫 26.0℃","low":"低溫 21.0℃","sunset":"18:58","aqi":53.0,"fx":"東南風","fl":"3-4級","type":"多云","notice":"陰晴之間斥扛,謹防紫外線侵擾"},{"date":"11日星期五","sunrise":"05:49","high":"高溫 28.0℃","low":"低溫 22.0℃","sunset":"18:59","aqi":45.0,"fx":"東風","fl":"3-4級","type":"陣雨","notice":"陣雨來襲,出門記得帶傘"}]}}
格式很亂對吧丹锹,沒關(guān)系稀颁,我們把它復(fù)制到GsonFormat窗口里format一下。
格式完成后就變成了這樣:
{
"date": "20180507",
"message": "Success !",
"status": 200,
"city": "廣州",
"count": 1642,
"data": {
"shidu": "86%",
"pm25": 14,
"pm10": 24,
"quality": "優(yōu)",
"wendu": "26",
"ganmao": "各類人群可自由活動",
"yesterday": {
"date": "06日星期日",
"sunrise": "05:52",
"high": "高溫 30.0℃",
"low": "低溫 24.0℃",
"sunset": "18:56",
"aqi": 44,
"fx": "無持續(xù)風向",
"fl": "<3級",
"type": "雷陣雨",
"notice": "帶好雨具楣黍,別在樹下躲雨"
},
"forecast": [
{
"date": "07日星期一",
"sunrise": "05:51",
"high": "高溫 27.0℃",
"low": "低溫 21.0℃",
"sunset": "18:57",
"aqi": 43,
"fx": "無持續(xù)風向",
"fl": "<3級",
"type": "大到暴雨",
"notice": "雨勢轉(zhuǎn)大匾灶,在外找好避雨處"
},
{
"date": "08日星期二",
"sunrise": "05:51",
"high": "高溫 26.0℃",
"low": "低溫 21.0℃",
"sunset": "18:57",
"aqi": 49,
"fx": "無持續(xù)風向",
"fl": "<3級",
"type": "大到暴雨",
"notice": "雨勢轉(zhuǎn)大,在外找好避雨處"
},
{
"date": "09日星期三",
"sunrise": "05:50",
"high": "高溫 25.0℃",
"low": "低溫 21.0℃",
"sunset": "18:58",
"aqi": 57,
"fx": "無持續(xù)風向",
"fl": "<3級",
"type": "中雨",
"notice": "記得隨身攜帶雨傘哦"
},
{
"date": "10日星期四",
"sunrise": "05:49",
"high": "高溫 26.0℃",
"low": "低溫 21.0℃",
"sunset": "18:58",
"aqi": 53,
"fx": "東南風",
"fl": "3-4級",
"type": "多云",
"notice": "陰晴之間锡凝,謹防紫外線侵擾"
},
{
"date": "11日星期五",
"sunrise": "05:49",
"high": "高溫 28.0℃",
"low": "低溫 22.0℃",
"sunset": "18:59",
"aqi": 45,
"fx": "東風",
"fl": "3-4級",
"type": "陣雨",
"notice": "陣雨來襲粘昨,出門記得帶傘"
}
]
}
}
這樣我們看是不是更舒服點?
GsonFormat的格式化除了幫助我們看清json數(shù)據(jù),還可以幫助我們檢查json數(shù)據(jù)格式是否正確哦窜锯。
從格式化后的代碼我們可以看到,這段json數(shù)據(jù)里的屬性很多芭析,而且有嵌套關(guān)系锚扎,如果要把這段json數(shù)據(jù)的屬性類全部寫出來,要費很大功夫馁启。
接下來就用到GsonFormat的另一個功能啦驾孔,就是幫我們生成Json數(shù)據(jù)里的屬性類。
首先我們自己新建一個類惯疙,叫WeatherBean, 然后在這個類里按alt+S打開GsonFormat翠勉,把json數(shù)據(jù)放進去,再點ok霉颠,json里的屬性類就都自動生成出來啦对碌。
具體操作如下:
有了屬性類之后,我們怎么將json數(shù)據(jù)的內(nèi)容解析出來呢蒿偎?
在要求返回一個bean類型的情況下朽们,如果是用JsonObject解析,我們要獲取數(shù)據(jù)->把數(shù)據(jù)內(nèi)容set到屬性類里诉位,單這兩部在遇到復(fù)雜json數(shù)據(jù)時就可以讓很多人頭疼了骑脱。
比如本文用到的json,用jsonObject的話可能要這樣寫:
public static WeatherBean getWeatherTm(String res) throws JSONException {
WeatherBean bean = new WeatherBean();
WeatherBean.DataBean dataBean = new WeatherBean.DataBean();
WeatherBean.DataBean.ForecastBean forecastBean = new WeatherBean.DataBean.ForecastBean();
List<WeatherBean.DataBean.ForecastBean> forecastBeanList = new ArrayList<>();
WeatherBean.DataBean.YesterdayBean yesterdayBean = new WeatherBean.DataBean.YesterdayBean();
JSONObject jsonObject = new JSONObject(res);
int jsStatus = jsonObject.getInt("status");
bean.setStatus(jsStatus);
String data = jsonObject.getString("data");
JSONObject jsonObject1 = new JSONObject(data);
String wendu = jsonObject1.getString("wendu");
dataBean.setWendu(wendu);
JSONObject jsonObject2 = jsonObject1.getJSONObject("yesterday");
String high = jsonObject2.getString("high");
yesterdayBean.setHigh(high);
JSONArray jsonArray = jsonObject1.getJSONArray("forecast");
for (int i = 0; i < jsonArray.length(); i++) {
forecastBean.setHigh(jsonArray.getJSONObject(i).getString("high"));
forecastBeanList.add(forecastBean);
}
dataBean.setForecast(forecastBeanList);
dataBean.setYesterday(yesterdayBean);
bean.setData(dataBean);
return bean;
}
上面還只是或取單個數(shù)據(jù),如果要求全部獲取呢苍糠?
當然這是我愚笨又夸張的寫法叁丧,也許JsonObject可以有更好的寫法。
但是用Gson的話,直接跳過set的過程拥娄,上面的代碼用Gson解析只需這樣:
public static WeatherBean getWeather(String res) {
Gson gson =new Gson();
WeatherBean wetherBean = gson.fromJson(res, WeatherBean.class);
return wetherBean;
}
gson.fromJson()就幫你做了所有set數(shù)據(jù)的內(nèi)容坷衍。還不用去苦惱嵌套時的數(shù)據(jù)獲取。
是不是很方便?
在邏輯代碼里獲取數(shù)據(jù)也只要get相應(yīng)的數(shù)據(jù)就行条舔。
以上就是使用Gson和GsonFormat解析復(fù)雜json數(shù)據(jù)的全部內(nèi)容啦枫耳。