您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。