0
我解析的地方特定格式的谷歌地方api。但是在解析後我得到了空值。我想要的地址格式如下:Mumbai, Maharashtra, IN, 400047
。爲此,我將參數types
的address_components
保存到ArrayList中,然後檢查特定的字符串。但是我的價值爲空。如何解析Google Places API的城市,州,國家代碼和Zip詳細信息?
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
if (jsonObject.getString("status").equals("OK")) {
JSONArray array = jsonObject.getJSONArray("results");
for(int k=0;k<array.length();k++){
JSONObject jObj = array.getJSONObject(0);
JSONArray jarr = jObj.getJSONArray("address_components");
for(int i=0; i<jarr.length();i++){
JSONObject jobj = jarr.getJSONObject(i);
String long_name = jobj.getString("long_name");
String short_name = jobj.getString("short_name");
JSONArray jarray = jobj.getJSONArray("types");
for(int j=0; j<jarray.length();j++){
typo.add(jarray.getString(j));
if(Arrays.asList(typo).contains("administrative_area_level_2")){
cityStr = long_name;
}else if(Arrays.asList(typo).contains("administrative_area_level_1")){
stateStr = long_name;
}else if(Arrays.asList(typo).contains("country")){
countryStr = short_name;
}else if(Arrays.asList(typo).contains("postal_code")){
zipStr = long_name;
}
}
}
}
result = cityStr + ", " + stateStr + ", " + zipStr + ", " + countryStr;
}