JSON响应值如下所示"types" : [ "sublocality", "political" ].如何获得类型的第一个值或如何获得单词sublocality?
Yar*_*lyk 15
String string = yourjson;
JSONObject o = new JSONObject(yourjson);
JSONArray a = o.getJSONArray("types");
for (int i = 0; i < a.length(); i++) {
Log.d("Type", a.getString(i));
}
Run Code Online (Sandbox Code Playgroud)
如果您只解析上面提供的行,这将是正确的.请注意,要从GoogleMaps地理编码访问类型,您应该获得一个结果数组,而不是address_components,然后您可以访问对象components.getJSONObject(index).
这是一个简单的实现,只解析formatted_address - 我在项目中需要的东西.
private void parseJson(List<Address> address, int maxResults, byte[] data)
{
try {
String json = new String(data, "UTF-8");
JSONObject o = new JSONObject(json);
String status = o.getString("status");
if (status.equals(STATUS_OK)) {
JSONArray a = o.getJSONArray("results");
for (int i = 0; i < maxResults && i < a.length(); i++) {
Address current = new Address(Locale.getDefault());
JSONObject item = a.getJSONObject(i);
current.setFeatureName(item.getString("formatted_address"));
JSONObject location = item.getJSONObject("geometry")
.getJSONObject("location");
current.setLatitude(location.getDouble("lat"));
current.setLongitude(location.getDouble("lng"));
address.add(current);
}
}
catch (Throwable e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36363 次 |
| 最近记录: |