查看: 2442|回复: 0

[Java语言] java web api json 数据解析

发表于 2018-1-18 08:00:02

1、jar 包
JSONObject 类需要导入两个json 包
1.1、json-20160810.jar
1.2、commons-io-2.4.jar

  1. File file = new File("C:" + File.separator + "curtis" + File.separator + "java" + File.separator + "demo"
  2. + File.separator + "JavaSE-01" + File.separator + "src" + File.separator + "weather.json");
  3. String content = FileUtils.readFileToString(file);
  4. JSONObject obj = new JSONObject(content);
  5. // System.out.println(obj);
  6. JSONObject today = obj.getJSONObject("result").getJSONObject("today");
  7. // System.out.println(obj.getJSONObject("result").getJSONObject("today"));
  8. System.out.print(today.getString("city") + " ");
  9. System.out.print(today.getString("week") + " ");
  10. System.out.print(today.getString("temperature"));
复制代码
  1. {
  2. "resultcode": "200",
  3. "reason": "successed!",
  4. "result": {
  5. "sk": {
  6. "temp": "9",
  7. "wind_direction": "东北风",
  8. "wind_strength": "3级",
  9. "humidity": "79%",
  10. "time": "14:15"
  11. },
  12. "today": {
  13. "temperature": "5℃~12℃",
  14. "weather": "中雨转小雨-中雨",
  15. "weather_id": {
  16. "fa": "08",
  17. "fb": "21"
  18. },
  19. "wind": "北风4-5 级",
  20. "week": "星期一",
  21. "city": "广州",
  22. "date_y": "2018年01月08日",
  23. "dressing_index": "较冷",
  24. "dressing_advice": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。",
  25. "uv_index": "最弱",
  26. "comfort_index": "",
  27. "wash_index": "不宜",
  28. "travel_index": "较不宜",
  29. "exercise_index": "较不宜",
  30. "drying_index": ""
  31. },
  32. "future": [
  33. {
  34. "temperature": "5℃~12℃",
  35. "weather": "中雨转小雨-中雨",
  36. "weather_id": {
  37. "fa": "08",
  38. "fb": "21"
  39. },
  40. "wind": "北风4-5 级",
  41. "week": "星期一",
  42. "date": "20180108"
  43. },
  44. {
  45. "temperature": "5℃~8℃",
  46. "weather": "小雨转阴",
  47. "weather_id": {
  48. "fa": "07",
  49. "fb": "02"
  50. },
  51. "wind": "北风3-4 级",
  52. "week": "星期二",
  53. "date": "20180109"
  54. },
  55. {
  56. "temperature": "6℃~14℃",
  57. "weather": "多云",
  58. "weather_id": {
  59. "fa": "01",
  60. "fb": "01"
  61. },
  62. "wind": "北风3-4 级",
  63. "week": "星期三",
  64. "date": "20180110"
  65. },
  66. {
  67. "temperature": "8℃~15℃",
  68. "weather": "多云",
  69. "weather_id": {
  70. "fa": "01",
  71. "fb": "01"
  72. },
  73. "wind": "微风",
  74. "week": "星期四",
  75. "date": "20180111"
  76. },
  77. {
  78. "temperature": "9℃~16℃",
  79. "weather": "多云",
  80. "weather_id": {
  81. "fa": "01",
  82. "fb": "01"
  83. },
  84. "wind": "微风",
  85. "week": "星期五",
  86. "date": "20180112"
  87. },
  88. {
  89. "temperature": "8℃~15℃",
  90. "weather": "多云",
  91. "weather_id": {
  92. "fa": "01",
  93. "fb": "01"
  94. },
  95. "wind": "微风",
  96. "week": "星期六",
  97. "date": "20180113"
  98. },
  99. {
  100. "temperature": "5℃~8℃",
  101. "weather": "小雨转阴",
  102. "weather_id": {
  103. "fa": "07",
  104. "fb": "02"
  105. },
  106. "wind": "北风3-4 级",
  107. "week": "星期日",
  108. "date": "20180114"
  109. }
  110. ]
  111. },
  112. "error_code": 0
  113. }
复制代码


回复

使用道具 举报