json数据类似:

  1. {
  2. "code": 0,
  3. "data": {
  4. "city": {
  5. "cityId": 284609,
  6. "counname": "中国",
  7. "name": "东城区",
  8. "pname": "北京市"
  9. },
  10. "liveIndex": {
  11. "2016-09-01": [
  12. {
  13. "day": "2016-09-01",
  14. "desc": "建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。",
  15. "name": "化妆指数",
  16. "status": "控油"
  17. },
  18. {
  19. "day": "2016-09-01",
  20. "desc": "感冒容易发生,少去人群密集的场所有利于降低感冒的几率。",
  21. "name": "感冒指数",
  22. "status": "易发"
  23. },
  24. {
  25. "day": "2016-09-01",
  26. "desc": "洗车后,可保持2天车辆清洁,比较适宜洗车。",
  27. "name": "洗车指数",
  28. "status": "较适宜"
  29. },
  30. {
  31. "day": "2016-09-01",
  32. "desc": "易感人群应适当减少室外活动。",
  33. "name": "空气污染扩散指数",
  34. "status": "中"
  35. },
  36. {
  37. "day": "2016-09-01",
  38. "desc": "天气较热,衣物精干简洁,室内酌情添加空调衫。",
  39. "name": "穿衣指数",
  40. "status": "热"
  41. },
  42. {
  43. "day": "2016-09-01",
  44. "desc": "辐射较弱,涂擦SPF12-15、PA+护肤品。",
  45. "name": "紫外线指数",
  46. "status": "弱"
  47. },
  48. {
  49. "day": "2016-09-01",
  50. "desc": "天气较好,且紫外线辐射不强,适宜户外运动。",
  51. "name": "运动指数",
  52. "status": "适宜"
  53. },
  54. {
  55. "day": "2016-09-01",
  56. "desc": "气压小幅波动,可能会影响鱼儿的进食。",
  57. "name": "钓鱼指数",
  58. "status": "较适宜"
  59. }
  60. ]
  61. }
  62. },
  63. "msg": "success",
  64. "rc": {
  65. "c": 0,
  66. "p": "success"
  67. }
  68. }
先导入fastjson依赖:

  1. & lt;!-- https: //mvnrepository.com/artifact/com.alibaba/fastjson --& gt;
  2. & lt;dependency& gt;
  3. & lt;groupId& gt;com.alibaba& lt; /groupId>
  4. <artifactId>fastjson</artifactId& gt;
  5. & lt;version& gt; 1.2. 33& lt; /version>
  6. </dependency& gt;

java拿出需要的数据:

  1. public static City getCity(String json){
  2. JSONObject jsonObject = (JSONObject) JSONObject.parseObject(json)
  3. .getJSONObject( "data")
  4. .getJSONObject( "city");
  5. City city = new City();
  6. city.setCityId(Integer.parseInt(String.valueOf(jsonObject.get( "cityId"))));
  7. city.setCityName((String) jsonObject.get( "pname"));
  8. city.setCunName((String)jsonObject.get( "counname"));
  9. city.setDistrictName((String)jsonObject.get( "name"));
  10. return city;
  11. }
  12. public static List<Life> getLife(String lifeJson){
  13. List list = new ArrayList();
  14. JSONObject jsonObject = (JSONObject) JSONObject.parseObject(lifeJson)
  15. .getJSONObject( "data")
  16. .getJSONObject( "liveIndex");
  17. JSONArray jsonArray = jsonObject.getJSONArray(getFormatDate());
  18. for( int i = 0; i < 8; i++){
  19. Life life = new Life();
  20. life.setDay( new Date());
  21. life.setDesc(jsonArray.getJSONObject(i).getString( "desc"));
  22. life.setName(jsonArray.getJSONObject(i).getString( "name"));
  23. life.setStatus(jsonArray.getJSONObject(i).getString( "status"));
  24. life.setCity(getCity(lifeJson));
  25. list.add(life);
  26. }
  27. return list;
  28. }
https://blog.csdn.net/eddie233/article/details/73730283



文章标签
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 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐