解析JSON文件Java

前端之家收集整理的这篇文章主要介绍了解析JSON文件Java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在 java中解析 JSON文件,并从下面提到的文件获取以下值:
  1. {
  2. "status": "OK","origin_addresses": [ "Vancouver,BC,Canada","Seattle,État de Washington,États-Unis" ],"destination_addresses": [ "San Francisco,Californie,États-Unis","Victoria,Canada" ],"rows": [ {
  3. "elements": [ {
  4. "status": "OK","duration": {
  5. "value": 340110,"text": "3 jours 22 heures"
  6. },"distance": {
  7. "value": 1734542,"text": "1 735 km"
  8. }
  9. },{
  10. "status": "OK","duration": {
  11. "value": 24487,"text": "6 heures 48 minutes"
  12. },"distance": {
  13. "value": 129324,"text": "129 km"
  14. }
  15. } ]
  16. },{
  17. "elements": [ {
  18. "status": "OK","duration": {
  19. "value": 288834,"text": "3 jours 8 heures"
  20. },"distance": {
  21. "value": 1489604,"text": "1 490 km"
  22. }
  23. },"duration": {
  24. "value": 14388,"text": "4 heures 0 minutes"
  25. },"distance": {
  26. "value": 135822,"text": "136 km"
  27. }
  28. } ]
  29. } ]
  30. }

从每个元素,我想获得距离和持续时间的值字段.我该怎么做?

解决方法

使用json.org参考实现( org.json homepage,Download here).代码有点乱,但我认为这是你要求的.您可以通过不创建所有这些对象来直接访问它们来获取大量快捷方式.我这样做的原因是试图让它更容易地遵循发生的事情.
  1. package com.mypackage;
  2.  
  3. import org.json.JSONArray;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. String jsonStr = "{\"status\": \"OK\",\"origin_addresses\": [ \"Vancouver,Canada\",\"Seattle,États-Unis\" ],\"destination_addresses\": [ \"San Francisco,États-Unis\",\"Victoria,Canada\" ],\"rows\": [ {\"elements\": [ {\"status\": \"OK\",\"duration\": {\"value\": 340110,\"text\": \"3 jours 22 heures\"},\"distance\": {\"value\": 1734542,\"text\": \"1 735 km\"}},{\"status\": \"OK\",\"duration\": {\"value\": 24487,\"text\": \"6 heures 48 minutes\"},\"distance\": {\"value\": 129324,\"text\": \"129 km\"}} ]},{\"elements\": [ {\"status\": \"OK\",\"duration\": {\"value\": 288834,\"text\": \"3 jours 8 heures\"},\"distance\": {\"value\": 1489604,\"text\": \"1 490 km\"}},\"duration\": {\"value\": 14388,\"text\": \"4 heures 0 minutes\"},\"distance\": {\"value\": 135822,\"text\": \"136 km\"}} ]} ]}";
  10.  
  11. try {
  12. JSONObject rootObject = new JSONObject(jsonStr); // Parse the JSON to a JSONObject
  13. JSONArray rows = rootObject.getJSONArray("rows"); // Get all JSONArray rows
  14.  
  15. for(int i=0; i < rows.length(); i++) { // Loop over each each row
  16. JSONObject row = rows.getJSONObject(i); // Get row object
  17. JSONArray elements = row.getJSONArray("elements"); // Get all elements for each row as an array
  18.  
  19. for(int j=0; j < elements.length(); j++) { // Iterate each element in the elements array
  20. JSONObject element = elements.getJSONObject(j); // Get the element object
  21. JSONObject duration = element.getJSONObject("duration"); // Get duration sub object
  22. JSONObject distance = element.getJSONObject("distance"); // Get distance sub object
  23.  
  24. System.out.println("Duration: " + duration.getInt("value")); // Print int value
  25. System.out.println("Distance: " + distance.getInt("value")); // Print int value
  26. }
  27. }
  28. } catch (JSONException e) {
  29. // JSON Parsing error
  30. e.printStackTrace();
  31. }
  32. }
  33. }

猜你在找的JavaScript相关文章