用fastjson解析嵌套json数据
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
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"
-
}
-
}
-
& lt;!-- https: //mvnrepository.com/artifact/com.alibaba/fastjson --& gt;
-
& lt;dependency& gt;
-
& lt;groupId& gt;com.alibaba& lt; /groupId>
-
<artifactId>fastjson</artifactId& gt;
-
& lt;version& gt; 1.2. 33& lt; /version>
-
</dependency& gt;
java拿出需要的数据:
-
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;
-
}
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:3 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献3条内容
所有评论(0)