2013-04-22 3 views
0

여기 Google Places API를 사용하여 특정 장소의 유형을 가져오고 싶습니다.하지만 분명히 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 
      } 

을하려고 무엇이며,이

"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" ] 
     }, 

그래서 당신은 내가이 문제에 대한 해결책을 알아내는 데 도움시겠습니까 JSON 응답입니까? 다음과 같이

+0

Google Places API를 호출 한 후 얻을 수있는 전체 JSON 형식은 무엇입니까? 그 대답에 당신이 얻는 것입니까? 게시하는 경우 답변하는 데 도움이됩니다. 또한 사람들이 JSON 응답을 구문 분석하려고 시도한 다른 질문을 보았습니까? 그것은 확실히 당신을 도울 것입니다. –

+0

예 여기서 잠시 동안 여기에서 검색해 보았습니다. 유용한 솔루션에 도달하지 못했습니다. –

+0

구문 분석하려는 JSON 응답이있는 게시물을 편집했습니다. –

답변

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에서 String을 추출하는 방법을 모르겠다. Joogle이 내게 보낸 유형을 Joğgetget()이라고 말할 수 없다는 이유만으로 –

+0

내가 직접 시도 했으므로 잘 작동한다. – buptcoder

+0

그게 형제 고마워. –

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는 유형 문자열을 저장합니다.

추신 :이 코드를 실행하지 않았습니다. 방금 문제 해결 방법을 알려 드리고자합니다. 기존 코드에 맞게 몇 가지 사항을 조정해야 할 수도 있습니다. 희망이 도움이됩니다.