我正在开发一个Android应用程序,它向Google提出请求(详细信息)并获取一个json对象作为回报。这是对象https://developers.google.com/places/documentation/details#PlaceDetailsResponses
`{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Road",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "NSW",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "AU",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"events" : [
{
"event_id" : "9lJ_jK1GfhX",
"start_time" : 1293865200,
"summary" : "<p>A visit from author John Doe, who will read from his latest book.</p>
<p>A limited number of signed copies will be available.</p>",
"url" : "http://www.example.com/john_doe_visit.html"
}
], ETC`
如何从json中获取具体细节。如果我尝试json.getJSONArray("result")
它告诉我json不是来自那种类型,或者如果我尝试json.getString("results")
它没有说明结果的价值。那么我如何提取信息,例如short_name等?
提前致谢
答案 0 :(得分:0)
你应该尝试
json.getJSONObject("result");
如何获取 short_name
JSONObject json=new JSONObject("json");
JSONObject result=json.getJSONObject("result");
JSONArray array=result.getJSONArray("address_components");
for(int i=0;i<array.length();i++)
{
JSONObject obj = array.getJSONObject(i);
String short_name=obj.getString("short_name");
System.out.println("Short Name:"+short_name);
}