您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java中要如何解析json報文,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
json報文如下:
{ "code": 0, "data": { "city": { "cityId": 284609, "counname": "中國", "name": "東城區", "pname": "北京市" }, "liveIndex": { "2016-09-01": [ { "day": "2016-09-01", "desc": "建議用露質面霜打底,水質無油粉底霜,透明粉餅,粉質胭脂。", "name": "化妝指數", "status": "控油" }, { "day": "2016-09-01", "desc": "感冒容易發生,少去人群密集的場所有利于降低感冒的幾率。", "name": "感冒指數", "status": "易發" }, { "day": "2016-09-01", "desc": "洗車后,可保持2天車輛清潔,比較適宜洗車。", "name": "洗車指數", "status": "較適宜" }, { "day": "2016-09-01", "desc": "易感人群應適當減少室外活動。", "name": "空氣污染擴散指數", "status": "中" }, { "day": "2016-09-01", "desc": "天氣較熱,衣物精干簡潔,室內酌情添加空調衫。", "name": "穿衣指數", "status": "熱" }, { "day": "2016-09-01", "desc": "輻射較弱,涂擦SPF12-15、PA+護膚品。", "name": "紫外線指數", "status": "弱" }, { "day": "2016-09-01", "desc": "天氣較好,且紫外線輻射不強,適宜戶外運動。", "name": "運動指數", "status": "適宜" }, { "day": "2016-09-01", "desc": "氣壓小幅波動,可能會影響魚兒的進食。", "name": "釣魚指數", "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 List<Life> getLife(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; }
上述內容就是Java中要如何解析json報文,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。