这可能是一个简单的 JSON 相关问题。我成功地从 Google Places API 接收到一个 JSON 对象,它看起来与 Google Places API 文档中的这个示例完全一样。我需要将每个地址组件(街道号码、城市、州、邮编等)分配给我的代码中的单独变量。例如,我如何最好地参考 street_number ?
这是我现在的做法,但它不起作用,因为例如,如果我的地址没有“楼层”,那么我将不得不引用address_components[0]而不是address_components[1]. 这是我现在无法使用的方式:
const MyStreetNumber = details.address_components[1].long_name
console.log(MyStreetNumber) //logs 48
这是“详细信息”的定义(我从 Google Places API 收到的响应):
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "5",
"short_name" : "5",
"types" : [ "floor" ]
},
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Rd",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Council of the City of Sydney",
"short_name" : "Sydney",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New South Wales",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Australia",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
拉莫斯之舞
慕田峪4524236
眼眸繁星
相关分类