2014-01-12 77 views
2

你好,大家好我正在閱讀一個網頁,包含具有JAVA(安卓)JSON對象使用JSON訪問特定的屬性

下面是該頁面包含:

{ 
    "destination_addresses" : [ "Safi, Maroc" ], 
    "origin_addresses" : [ "Avenue Hassan II, Safi, Maroc" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "1,0 km", 
        "value" : 966 
       }, 
       "duration" : { 
        "text" : "2 minutes", 
        "value" : 129 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

我知道如何閱讀從這樣

JSONObject jArray = new JSONObject(result); 
    String elements = jArray.getString("rows"); 

元素串的頁面包含:

[{"elements" : [{"distance" : {"text" : "1,0 km","value" : 966},"duration" : {"text" : "2 minutes","value" : 129},"status" : "OK"}]}] 

但是我試圖讓距離值這是 「」

THX傢伙

回答

1

試試這個..

JSONObject jObj = new JSONObject(result); 
JSONArray rows = jObj.getJSONArray("rows"); 
for(int i = 0; i < rows.length; i++){ 
    JSONObject obj = rows.getJSONObject(i); 
    JSONArray elements = jObj.getJSONArray("elements"); 
    for(int j = 0; j < elements.length; j++){ 
      JSONObject Jobj = elements.getJSONObject(j); 
      JSONObject distance = Jobj.getJSONObject("distance"); 
      int distance_value = distance.getInteger("value"); 
      JSONObject duration = Jobj.getJSONObject("duration"); 
      int duration_value = duration.getInteger("value"); 
    } 
} 
2

你可能想看看這個鏈接:

How to parse JSON in Android

但是對於你想要調用的特定問題:

int value = jArray.getJSONObject("rows").getJSONObject("elements").getJSONArray("distance").getInteger("value"); 
+0

是這項工作,TY – Coldfire