2016-01-21 71 views
-2
{ 
    "destination_addresses" : [ "Bombay, Maharashtra, Inde" ], 
    "origin_addresses" : [ "New Delhi, New Delhi 110001, Inde" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "1 457 km", 
        "value" : 1457222 
       }, 
       "duration" : { 
        "text" : "22 heures 8 minutes", 
        "value" : 79663 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 
+0

你寫的代碼只。沒有任何關於你想做什麼的解釋,我怎能甚至試圖幫助你? – phaberest

+0

解析到ListView?看看這個:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ –

回答

0

請使用以下代碼作爲參考。

JSONObject jsonObject = new JSONObject("jsonResponse"); 
    JSONArray jsonArr= jsonObj.getJSONArray("destination_addresses"); 
    String[] arrDestinations=new String[jsonArr.lLength()]; 
    for(int i=0;i<jsonArr.length();i++) 
     arrDestinations[i]=jsonArr.get(i); 

    jsonArr= jsonObj.getJSONArray("origin_addresses"); 
    String[] arrOrigins=new String[jsonArr.lLength()]; 
    for(int i=0;i<jsonArr.length();i++) 
     arrOrigins[i]=jsonArr.get(i); 

    JSONArray rows = jsonObj.getJSONArray("rows"); 
    for(int i =0; i < rows.length(); i++) 
    { 
     JSONObject rowObj = rows.getJSONObject(i); 
     JSONArray rowElements = rowObj.getJSONArray("elements"); 
     for(int element = 0; element < rowElements.length(); element++) { 
       // get the elements and parse 
     } 
    } 

希望這會有所幫助。

+0

謝謝你回覆我已經追蹤了json文檔:) –

+0

如果這對你有幫助,那麼請upvote並接受答案:) –

0

檢查您的Logcat以查看JsonObject中提取的值。

 try { 
      JSONObject jsonObj = new JSONObject(JsonObject.toString()); 

      JSONArray destination_addresses = jsonObj.getJSONArray("destination_addresses"); 
      for (int i = 0; i < destination_addresses.length(); i++) { 
       Log.e("Values", "destination_addresses = " + destination_addresses.get(i)); 
      } 

      JSONArray origin_addresses = jsonObj.getJSONArray("origin_addresses"); 
      for (int i = 0; i < origin_addresses.length(); i++) { 
       Log.e("Values", "origin_addresses = " + origin_addresses.get(i)); 
      } 

      JSONArray rows = jsonObj.getJSONArray("rows"); 
      for (int i = 0; i < rows.length(); i++) { 
       JSONObject rowObject = rows.getJSONObject(i); 
       JSONArray rowElements = rowObject.getJSONArray("elements"); 
       for (int j = 0; j < rowElements.length(); j++) { 
        JSONObject elementObject = rowElements.getJSONObject(j); 
        JSONObject distanceObject = elementObject.getJSONObject("distance"); 
        String distanceText = distanceObject.getString("text"); 
        String distanceValue = distanceObject.getString("value"); 

        JSONObject durationObject = elementObject.getJSONObject("duration"); 
        String durationText = distanceObject.getString("text"); 
        String durationValue = distanceObject.getString("value"); 
        Log.e("Values", "distanceText = " + distanceText + "\ndistanceValue = " + distanceValue + "\ndurationText = " + durationText + "\ndurationValue = " + durationValue); 
       } 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
+0

感謝您的回覆我已追蹤到JSON文檔:) –

+0

歡迎:) @HarshaVardhan –

+0

第一件事我已經將它轉換成json Hirarchy樹,然後我明白解析:)此工具確實有助於http ://basic4ppc.com:51042/JSON/index.html的 –