2015-04-03 12 views
5

Ich bin neu in Android-Entwicklung und neu in JSON. Ich benutze die Google Maps Entfernungsmatrix API. Ich habe den JSON-Download in ein JSONObject richtig gemacht, glaube ich. (Ich habe den Code gestohlen, um es aus einem anderen Post zu machen). Allerdings kann ich den JSON nicht richtig parsen. Ich arbeite seit ein paar Tagen daran und bin völlig ratlos. Ich mache das folgende Aufruf unterParsen von JSON aus der Google Maps DistanceMatrix API in Android

https://maps.googleapis.com/maps/api/distancematrix/json?origins=1600%20pennsylvania%20avenue&destinations=1500%20college%20street&mode=driving&units=imperial 

Der Ausgang Google ist dies:

{ 
    "destination_addresses" : [ "1500 College Street, Beaumont, TX 77701, USA" ], 
    "origin_addresses" : [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ], 
    "rows" : [ 
     { 
     "elements" : [ 
      { 
       "distance" : { 
        "text" : "1,306 mi", 
        "value" : 2102536 
       }, 
       "duration" : { 
        "text" : "18 hours 48 mins", 
        "value" : 67684 
       }, 
       "status" : "OK" 
      } 
     ] 
     } 
    ], 
    "status" : "OK" 
} 

ich versucht habe:

1.

JSONArray jsonArray = jObject.getJSONArray("rows"); 
JSONArray routes = jsonArray.getJSONArray(0); 
stringBuilder.append(routes.getJSONObject(0).getString("text")); 

2.

JSONArray jsonArray = jObject.getJSONArray("rows"); 
JSONObject routes = jsonArray.getJSONObject(0); 
stringBuilder.append(routes.getJSONObject("distance").getString("text")); 

3.

JSONArray jsonArray = jObject.getJSONArray("elements");     stringBuilder.append(routes.getJSONObject(0).getString("text")); 

Ich habe versucht, mehr aber die scheinen mir wie sie funktionieren sollte. Es schien mir, dass Zeilen ein Array und Elemente auch ein Array sind. Also würde es folgen, dass ich Zeilen aus dem ursprünglichen JSONObject abrufen müsste, dann das Elementarray aus dem Zeilenarray holen und dann das Distanzobjekt aus diesem Array holen, dann den Textwert schließlich abrufen und ihn dem String Builder I hinzufügen früher erstellt. War ich falsch? Vielen Dank im Voraus für jede Hilfe.

+0

* Ich habe fast alles versucht und es wirft verschiedene Ausnahmen bekommen ... auch, bevor ich Google nach diesen Ausnahmen frage ... – Selvin

+0

Ich habe es bearbeitet, um etwas davon zu zeigen. Ich habe die Ausnahmen googlen. Aber ich dachte nicht, dass es einen Sinn ergeben würde. Zum Beispiel, falls es das Ding sagt in Position 0 des jsonArray-Objekts ist kein Array.Wäre das Ding aber nicht Elemente, die ein Array sind? – eam1813

+0

'rows' ist ein Array. dann * erstes Element * ist ein Objekt, dann' elements' -Eigenschaft dieses Objekts ist ein Array, dann ist * das erste Element * ein Objekt mit der Eigenschaft 'distance' ... welches ein Objekt ist, das ' text' property ... in 2nd bist du fast da ... aber du hast 'elements' array vergessen – Selvin

Antwort

8

ist hier, was mit mir gearbeitet

//httpResponse is the output of google api 
JSONObject jsonRespRouteDistance = new JSONObject(httpResponse) 
             .getJSONArray("rows") 
             .getJSONObject(0) 
             .getJSONArray ("elements") 
             .getJSONObject(0) 
             .getJSONObject("distance"); 

String distance = jsonRespRouteDistance.get("text").toString(); 

/* 
* For distance, below is only partial solution as the 
* output to string destination_addr will contain square brackets [] and double codes "" 
* Eg. [ "1600 Pennsylvania Avenue, Hagerstown, MD 21742, USA" ] 
* 
*/ 
String destination_addr = new JSONObject(httpResponse) 
          .get("destination_addresses") 
          .toString(); 

[UPDATE]

wir nur eine Zieladresse Unter der Annahme haben und nicht mehrere. Eine kleine String-Manipulation bringt uns die saubere String-Zeichenfolge ohne Codes "und Klammern []

StringBuilder stringBuilderDestinationAddr = new StringBuilder(); 

for (int i = 0; i < destination_addr.length(); i++) 
    if (destination_addr.charAt(i) != '[' && destination_addr.charAt(i) != ']' && destination_addr.charAt(i) != '"') 
     stringBuilderDestinationAddr.append(pickup_addr.destination_addr (i)); 

String strCleanDestination = stringBuilderDestinationAddr.toString();