2016-10-11 132 views
0

我想從這個數據中獲取數據,但它很混亂,怎麼做。 我想訪問腿。在腿部距離然後文本。 然後在步驟之後,在步驟再次距離然後文本。從Json獲取多個對象Android

在 「路線」 ==> 「腿」 - >距離 - >文本

然後獲得驅動步進 「路線」 ==> 「腿」 ==> 「臺階」 - >距離 - >文本

首先有對象,然後數組再次對象它太混亂。任何幫助,將不勝感激。

{ 
     "geocoded_waypoints" : [ 
      { 
      "geocoder_status" : "OK", 
      "place_id" : "ChIJ2QeB5YMEGTkRYiR-zGy-OsI", 
      "types" : [ "locality", "political" ] 
      }, 
      { 
      "geocoder_status" : "OK", 
      "place_id" : "ChIJ2afeeFcxOzkRL9RVTscv17o", 
      "types" : [ "locality", "political" ] 
      } 
     ], 
     "routes" : [ 
      { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 31.55462439999999, 
        "lng" : 74.3571711 
       }, 
       "southwest" : { 
        "lat" : 30.1981178, 
        "lng" : 71.4687352 
       } 
      }, 
      "copyrights" : "Map data ©2016 Google", 
      "legs" : [ 
       { 
        "distance" : { 
         "text" : "348 km", 
         "value" : 347978 
        }, 
        "duration" : { 
         "text" : "4 hours 49 mins", 
         "value" : 17335 
        }, 
        "end_address" : "Multan, Pakistan", 
        "end_location" : { 
         "lat" : 30.1981178, 
         "lng" : 71.4687352 
        }, 
        "start_address" : "Lahore, Pakistan", 
        "start_location" : { 
         "lat" : 31.55462439999999, 
         "lng" : 74.3571711 
        }, 
        "steps" : [ 
         { 
         "distance" : { 
          "text" : "67 m", 
          "value" : 67 
         }, 
         "duration" : { 
          "text" : "1 min", 
          "value" : 9 
         }, 
         "end_location" : { 
          "lat" : 31.5549532, 
          "lng" : 74.3565735 
         }, 
         "html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eAllama Iqbal Rd\u003c/b\u003e", 
         "polyline" : { 
          "points" : "k_r_Ei{ydM[[email protected]@bA" 
         }, 
         "start_location" : { 
          "lat" : 31.55462439999999, 
          "lng" : 74.3571711 
         }, 
         "travel_mode" : "DRIVING" 
         }, 
+0

我用GSON爲,沒什麼可擔心的,易於使用=),如果你想我可以給你舉個例子。或者嘗試先閱讀它? –

+0

參考http://stackoverflow.com/questions/17673057/how-to-parse-this-nested-json-array-in-android – sasikumar

回答

2

手冊方法:

在幾個字的Json給你的對象(beetwen {})或陣列(beetwen [])。爲了深入研究json,你必須通過每個級別。你把JSON的只是片段,但我關閉它,我們在開始的JSON對象(因爲主要括號{}):

{ 
    "geocoded_waypoints" : [ 
     { 
     "geocoder_status" : "OK", 
     "place_id" : "ChIJ2QeB5YMEGTkRYiR-zGy-OsI", 
     "types" : [ "locality", "political" ] 
     }, 
     { 
     "geocoder_status" : "OK", 
     "place_id" : "ChIJ2afeeFcxOzkRL9RVTscv17o", 
     "types" : [ "locality", "political" ] 
     } 
    ], 
    "routes" : [ 
     { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 31.55462439999999, 
       "lng" : 74.3571711 
      }, 
      "southwest" : { 
       "lat" : 30.1981178, 
       "lng" : 71.4687352 
      } 
     }, 
     "copyrights" : "Map data ©2016 Google", 
     "legs" : [ 
      { 
       "distance" : { 
        "text" : "348 km", 
        "value" : 347978 
       }, 
       "duration" : { 
        "text" : "4 hours 49 mins", 
        "value" : 17335 
       }, 
       "end_address" : "Multan, Pakistan", 
       "end_location" : { 
        "lat" : 30.1981178, 
        "lng" : 71.4687352 
       }, 
       "start_address" : "Lahore, Pakistan", 
       "start_location" : { 
        "lat" : 31.55462439999999, 
        "lng" : 74.3571711 
       }, 
       "steps" : [ 
        { 
        "distance" : { 
         "text" : "67 m", 
         "value" : 67 
        }, 
        "duration" : { 
         "text" : "1 min", 
         "value" : 9 
        }, 
        "end_location" : { 
         "lat" : 31.5549532, 
         "lng" : 74.3565735 
        }, 
        "html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eAllama Iqbal Rd\u003c/b\u003e", 
        "polyline" : { 
         "points" : "k_r_Ei{ydM[[email protected]@bA" 
        }, 
        "start_location" : { 
         "lat" : 31.55462439999999, 
         "lng" : 74.3571711 
        }, 
        "travel_mode" : "DRIVING" 
        }] 
      } 
      ] 
     } 
    ] 

}

比方說,你有這樣的一個名爲「jsonString字符串」。創建JSON對象是這樣的:

JSONObject jsonObject = new JSONObject(jsonString); 

比你需要 「路線」,也就是JSON陣列(括號之間[])。創建這個數組深入到你的主要的JSONObject:

JSONArray routesArray = jsonObject.getJSONArray("routes"); 

要經過每個數組元素(即是JSON對象),你重複這樣的:

for (int i = 0; i < routesArray.length(); i++) { 
    //do sth 
} 

,你會得到你的「腿」狀這個:

JSONObject routeObject = routesArray.get(i); 
routeObject.get("legs"); 

和上面一樣更深更深。

自動

它更容易使用GSON圖書館和一些Json的POJO的解析器。

你在你的build.gradle導入GSON:

compile 'com.google.code.gson:gson:2.4' 

比你可以使用這個parser。選擇源類型「JSON」和「註釋樣式」Gson。把你的json放入文本框並點擊預覽或下載ZIP。您也可以將您的軟件包名稱和主類名稱或稍後手動重命名。您將獲得多個模型類,其中一個名爲「示例」或您的名字。它包括兩個字段:

public class Example { 
@SerializedName("geocoded_waypoints") 
@Expose 
private List<GeocodedWaypoint> geocodedWaypoints = new ArrayList<GeocodedWaypoint>(); 
@SerializedName("routes") 
@Expose 
private List<Route> routes = new ArrayList<Route>(); 

添加所有的類到您的項目,比你剛纔分析你jsonString到主類是這樣的:

Example mainClassObject = new Gson().fromJson(jsonString, Example.class); 

現在你可以從這個對象訪問路線。來自路線和更深更深的腿

0

請嘗試以下代碼來解析提供JSON的項值:

private void parseJson(String jsonString){ 
     ArrayList<String> legsDistanceList = new ArrayList<>(); 
     ArrayList<String> legStepsDistanceList = new ArrayList<>(); 
     try{ 
      JSONObject rootJson = new JSONObject(jsonString); 
      if(rootJson.has("routes")) { 
       JSONArray routesJsonArray = rootJson.getJSONArray("routes"); 
       for(int i=0;i<routesJsonArray.length();i++){ 
        JSONObject routeJson = routesJsonArray.getJSONObject(i); 
        if(routeJson.has("legs")){ 
         JSONArray legsJsonArray = routeJson.getJSONArray("legs"); 
         for(int j=0;j<legsJsonArray.length();j++){ 
          JSONObject legJson = legsJsonArray.getJSONObject(j); 
          if(legJson.has("distance")){ 
           JSONObject distanceJson = legJson.getJSONObject("distance"); 
           legsDistanceList.add(distanceJson.getString("text")); 
          } 
          if(legJson.has("steps")){ 
           JSONArray stepsJsonArray = legJson.getJSONArray("steps"); 
           for(int k=0;k<stepsJsonArray.length();k++){ 
            JSONObject stepJson = stepsJsonArray.getJSONObject(k); 
            if(stepJson.has("distance")){ 
             JSONObject stepDistanceJson = stepJson.getJSONObject("distance"); 
             legStepsDistanceList.add(stepDistanceJson.getString("text")); 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     }catch (Throwable e){ 
      e.printStackTrace(); 
     } 

     Log.i("Legs Distance ",legsDistanceList.toString()); 
     Log.i("Legs Steps Distance ",legStepsDistanceList.toString()); 
    } 

我覺得上面的代碼可能是你很難在這個階段理解,但一旦你那麼瞭解它會更有利於你將來的JSON解析東西。

+0

它確實有幫助,你已經使代碼很容易,謝謝。 – Mamoon

0

您可以通過第三方(Gson,Logan Square等)進行解析。如果您試圖直接訪問它。在下面你可以找到示例代碼來訪問「腿」數組。

讓我們假設

JsonObject jsonObject = new JsonObject("json string"); 

這個「的JSONObject」是完整的JSON值,從中你要訪問的「腿」陣列。在這種情況下,「腿」數組在「路線」數組內。所以我們需要首先獲取「routes」數組並循環該數組,並從每個對象獲取「legs」數組並存儲在POJO類中,或者如果要從「distance」中單獨獲取「text」,則意味着您可以存儲那在你需要arraylist或字符串。以下是單獨獲取文本的示例代碼。

JsonArray routesArray = jsonObject.optJsonArray("routes"); 
     if (routesArray != null) { 
      for (int i = 0; i < routesArray.length(); i++) { 
       JsonArray legsArray = routesArray.get(i).optJsonArray("legs"); 
       if (legsArray != null) { 
        for (int i = 0; i < legsArray.length(); i++) { 
         JsonObject legObject = legsArray.get(i); 
         JsonObject distanceObject = legObject.optJsonObject("distance"); 
         String textValue = distanceObject.optString("text"); 
        } 
       } 
      } 
     } 

希望這是有益:)