本篇文章為大家展示了Java中要如何解析json報(bào)文,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
為陵川等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及陵川網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站建設(shè)、做網(wǎng)站、陵川網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
json報(bào)文如下:
{ "code": 0, "data": { "city": { "cityId": 284609, "counname": "中國(guó)", "name": "東城區(qū)", "pname": "北京市" }, "liveIndex": { "2016-09-01": [ { "day": "2016-09-01", "desc": "建議用露質(zhì)面霜打底,水質(zhì)無(wú)油粉底霜,透明粉餅,粉質(zhì)胭脂。", "name": "化妝指數(shù)", "status": "控油" }, { "day": "2016-09-01", "desc": "感冒容易發(fā)生,少去人群密集的場(chǎng)所有利于降低感冒的幾率。", "name": "感冒指數(shù)", "status": "易發(fā)" }, { "day": "2016-09-01", "desc": "洗車(chē)后,可保持2天車(chē)輛清潔,比較適宜洗車(chē)。", "name": "洗車(chē)指數(shù)", "status": "較適宜" }, { "day": "2016-09-01", "desc": "易感人群應(yīng)適當(dāng)減少室外活動(dòng)。", "name": "空氣污染擴(kuò)散指數(shù)", "status": "中" }, { "day": "2016-09-01", "desc": "天氣較熱,衣物精干簡(jiǎn)潔,室內(nèi)酌情添加空調(diào)衫。", "name": "穿衣指數(shù)", "status": "熱" }, { "day": "2016-09-01", "desc": "輻射較弱,涂擦SPF12-15、PA+護(hù)膚品。", "name": "紫外線(xiàn)指數(shù)", "status": "弱" }, { "day": "2016-09-01", "desc": "天氣較好,且紫外線(xiàn)輻射不強(qiáng),適宜戶(hù)外運(yùn)動(dòng)。", "name": "運(yùn)動(dòng)指數(shù)", "status": "適宜" }, { "day": "2016-09-01", "desc": "氣壓小幅波動(dòng),可能會(huì)影響?hù)~(yú)兒的進(jìn)食。", "name": "釣魚(yú)指數(shù)", "status": "較適宜" } ] } }, "msg": "success", "rc": { "c": 0, "p": "success" } }
java解析json代碼:
public static City getCity(String json){ JSONObject jsonObject = (JSONObject) JSONObject.parseObject(json) .getJSONObject("data") .getJSONObject("city"); City city = new City(); city.setCityId(Integer.parseInt(String.valueOf(jsonObject.get("cityId")))); city.setCityName((String) jsonObject.get("pname")); city.setCunName((String)jsonObject.get("counname")); city.setDistrictName((String)jsonObject.get("name")); return city; } public static ListgetLife(String lifeJson){ List list = new ArrayList(); JSONObject jsonObject = (JSONObject) JSONObject.parseObject(lifeJson) .getJSONObject("data") .getJSONObject("liveIndex"); JSONArray jsonArray = jsonObject.getJSONArray(getFormatDate()); for(int i = 0; i < 8; i++){ Life life = new Life(); life.setDay(new Date()); life.setDesc(jsonArray.getJSONObject(i).getString("desc")); life.setName(jsonArray.getJSONObject(i).getString("name")); life.setStatus(jsonArray.getJSONObject(i).getString("status")); life.setCity(getCity(lifeJson)); list.add(life); } return list; }
上述內(nèi)容就是Java中要如何解析json報(bào)文,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。