2013-04-22 80 views
0

這裏我想要使用Google Places API獲取特定位置的類型,但是Im顯然在如何從JSON格式中獲取所需的「類型」值方面存在巨大問題。來自Google Places的JSON

這裏是我試圖做

JSONObject predictions = new JSONObject(sb.toString()); 
      JSONArray ja = new JSONArray(predictions.getString("results")); 
      JSONArray types = ((JSONObject)ja.get(0)).getJSONArray("types"); 
      for (int i = 0; i < types.length(); i++) { 
       JSONObject jo = (JSONObject) ja.get(i); 
       //here I stop 
      } 

,這是JSON響應

"results" : [ 
     { 
     "formatted_address" : "529 Kent Street, Sydney NSW, Australia", 
     "geometry" : { 
      "location" : { 
       "lat" : -33.8750460, 
       "lng" : 151.2052720 
      } 
     }, 
     "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png", 
     "id" : "827f1ac561d72ec25897df088199315f7cbbc8ed", 
     "name" : "Tetsuya's", 
     "rating" : 4.30, 
     "reference" : "CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg", 
     "types" : [ "restaurant", "food", "establishment" ] 
     }, 

所以請你幫我找出一個解決這個問題?謝謝

+0

撥打Google Places API後得到的整個JSON格式是什麼?這是你得到的迴應?如果您發佈該信息,這將有助於回答。您是否還嘗試過在其他人嘗試解析JSON響應的問題上看到其他問題?它肯定會幫助你。 – 2013-04-22 01:28:51

+0

是的,我一直在這裏搜索了一段時間,我沒有達到有用的解決方案。 – 2013-04-22 01:37:17

+0

我編輯了我想要解析的JSON響應文章 – 2013-04-22 01:39:03

回答

1
String testData = "{\"results\" : [ " 
      + "{\"formatted_address\" : \"529 Kent Street, Sydney NSW, Australia\", " 
      + "\"geometry\" : {" 
      + "\"location\" : {" 
      + "\"lat\" : -33.8750460," 
      + "\"lng\" : 151.2052720}" 
      + "}," 
      + "\"icon\" : \"http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png\"," 
      + "\"id\" : \"827f1ac561d72ec25897df088199315f7cbbc8ed\", " 
      + "\"name\" : \"Tetsuya's\"," 
      + "\"rating\" : 4.30," 
      + "\"reference\" : \"CnRmAAAAmmm3dlSVT3E7rIvwQ0lHBA4sayvxWEc4nZaXSSjRtfKRGoYnfr3d5AvQGk4e0u3oOErXsIJwtd3Wck1Onyw6pCzr8swW4E7dZ6wP4dV6AsXPvodwdVyqHgyGE_K8DqSp5McW_nFcci_-1jXb5Phv-RIQTzv5BjIGS0ufgTslfC6dqBoU7tw8NKUDHg28bPJlL0vGVWVgbTg\", " 
      + "\"types\" : [ \"restaurant\", \"food\", \"establishment\" ] " 
      + "}] }"; 
    JSONObject predictions; 
    try { 
     predictions = new JSONObject(testData); 
     JSONArray ja = (JSONArray) predictions.get("results"); 
     JSONArray types = (JSONArray) ((JSONObject) ja.get(0)).get("types"); 
     String result = types.toString(); 
     result = result.substring(1, result.length() - 1); 
     String[] allTypes = result.split(","); 
     for(int i = 0; i < allTypes.length;i++) { 
      String type = allTypes[i]; 
      //save your type 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

是的,這是我的問題!我不知道如何從這些JSONObjects中提取字符串,前提是我不能說Jo.getString(),因爲我不知道從google發送給我的類型 – 2013-04-22 10:01:08

+0

我自己試過了,它運行良好 – buptcoder 2013-04-23 02:19:53

+0

我感謝兄弟 – 2013-11-17 09:24:23

0
//Supposing that you get the complete response as one JSONObject called 'res' as follows: 

JSONObject res = new JSONObject(); 
try { 
    res = new JSONObject(stringBuilder.toString()); // stringBuilder is string which you append everthing when you are reading the stream 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 

您可以從所得到的「水庫」 JSON對象如下抽取對應於「類型」的值:

JSONObject type; 
String type_string; 
try { 
    type = ret.getJSONArray("results").getJSONObject(0); 
    type_string = type.getString("types"); 
    Log.d("test", "formattted address:" + location_string); 

} catch (JSONException e1) { 
    e1.printStackTrace(); 

} 

在「TYPE_STRING」你應該有值的字符串,可以在

[ 「餐廳」, 「食品」, 「建立」]

: '類型',即 像前面

然後,您可以使用.Split()函數使用「,」作爲分隔符來分割字符串。例如:

String[] str_array = type_string.split(","); 
String stringa = str_array[0]; 
String stringb = str_array[1]; 
String stringC = str_array[2]; 

上述str_array將存儲類型字符串。

P.S:我沒有運行這段代碼。我只是想給你一個想法如何解決你的問題。你可能需要調整一些東西來適應你現有的代碼。希望能幫助到你。