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;
}
原文地址:https://www.cnblogs.com/wszn-java/p/11880978.html
时间: 2024-10-12 23:57:42