【发布时间】:2015-04-26 06:05:54
【问题描述】:
我像这样从谷歌地图 api 检索一个 json
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address="+data.SimpleProfileForm.postcode+"&sensor=false")
.success(function(data){
console.log(data);
user.location = data.results.0.address_components.3.long_name;
})
.error(function(data){
console.log("there was an error with the postcode API");
console.log(data);
})
但我在这一行得到错误的意外数字:
user.location = data.results.0.address_components.3.long_name;
如果我删除我得到的数字'无法读取未定义的属性“long_name”'。
我如何访问数组中的数据?提前致谢, 克里斯
【问题讨论】:
-
你能提供json响应吗?从这些信息中我们无法判断响应的结构
-
如果是数组,则需要使用方括号表示法来访问其元素:
user.location = data.results[0].address_components[3].long_name;点表示法用于对象的非数字属性。
标签: javascript arrays json angularjs google-maps-api-3