2012-01-20 222 views
12

我想分析Java中的JSON文件,並從文件中的下列值提到如下:解析JSON文件的Java

{ 
    "status": "OK", 
    "origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ], 
    "destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, 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" 
     } 
    }, { 
     "status": "OK", 
     "duration": { 
     "value": 14388, 
     "text": "4 heures 0 minutes" 
     }, 
     "distance": { 
     "value": 135822, 
     "text": "136 km" 
     } 
    } ] 
    } ] 
} 

從每一個元素,我想距離和持續時間的值字段。我該怎麼做呢?

+1

檢查:http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-成-一個陣列 – aayoubi

回答

10

使用json.org參考實現(org.json homepageDownload here)。代碼有點混亂,但我認爲它符合你的要求。您可以通過不創建所有這些對象而直接訪問它們來獲取大量快捷方式。我這樣做的原因是爲了更容易地跟蹤發生的事情。

package com.mypackage; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class Main { 
    public static void main(String[] args) { 
     String jsonStr = "{\"status\": \"OK\",\"origin_addresses\": [ \"Vancouver, BC, Canada\", \"Seattle, État de Washington, États-Unis\" ],\"destination_addresses\": [ \"San Francisco, Californie, États-Unis\", \"Victoria, BC, 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\"}}, {\"status\": \"OK\",\"duration\": {\"value\": 14388,\"text\": \"4 heures 0 minutes\"},\"distance\": {\"value\": 135822,\"text\": \"136 km\"}} ]} ]}"; 

     try { 
      JSONObject rootObject = new JSONObject(jsonStr); // Parse the JSON to a JSONObject 
      JSONArray rows = rootObject.getJSONArray("rows"); // Get all JSONArray rows 

      for(int i=0; i < rows.length(); i++) { // Loop over each each row 
       JSONObject row = rows.getJSONObject(i); // Get row object 
       JSONArray elements = row.getJSONArray("elements"); // Get all elements for each row as an array 

       for(int j=0; j < elements.length(); j++) { // Iterate each element in the elements array 
        JSONObject element = elements.getJSONObject(j); // Get the element object 
        JSONObject duration = element.getJSONObject("duration"); // Get duration sub object 
        JSONObject distance = element.getJSONObject("distance"); // Get distance sub object 

        System.out.println("Duration: " + duration.getInt("value")); // Print int value 
        System.out.println("Distance: " + distance.getInt("value")); // Print int value 
       } 
      } 
     } catch (JSONException e) { 
      // JSON Parsing error 
      e.printStackTrace(); 
     } 
    } 
} 
6
  1. 創建一個類結構,反映了JSON
  2. 使用像傑克遜或GSON庫的JSON反序列化到你的類的實例。

如果你想要一個更動態的方法,上面的框架也可以序列化爲映射。

1

爲Bozho說創建類結構reflacts JSON,然後用jacson庫如下:

參考Parsing JSON File Java

ObjectMapper mapper = new ObjectMapper(); 
Projects projs = 
mapper.readValue(new File("projects.json"),Projects.class); 
ArrayList<Project> projects = projs.get("projects"); 
for (Project p : projects) { 
ArrayList<String> description = p.getDescription(); 
for (String s : description) { 
System.out.println(s); 
0

您可以使用圖書館像JSON-java

import org.json.JSONObject; 

String jsonString = ... 
JSONObject object = new JSONObject(jsonString); 
String status = object.getString("status"); 
-1

是你可以使用jacson來解析它,但有更簡單的方法來做到這一點 它的Jsonme庫「import org.json.me」你不必廣告d jar文件使用它

 JSONObject obj = new JSONObject("{'var1':'val1','var2':200}); 
    String var1=obj.getString("var1"); 
    int var2=obj.getInt("var2"); 

是其更容易,但如果你的項目很複雜我建議你使用jacson LIB

0

您可以使用:

org.bson.Document d = org.bson.Document.parse("{ foo: \"bar\" }");