我尝试了(Chrome)浏览器控制台中的示例,但我忽略了“类型注释”。你的第二个访问示例对我有效,我不确定它为什么会失败。
var o = {
"destination_addresses": [
"21 Foo St, SomeCity, SomeState 33333, USA",
],
"origin_addresses": [
"5555 Somewhere Dr, Somewhere, Somewhere 55555, USA",
],
"rows": [
{
"elements": [
{
"distance": {
"text": "2,302 mi",
"value": 3703935,
},
"duration": {
"text": "1 day 10 hours",
"value": 123162,
},
"status": "OK",
},
],
},
],
"status": "OK",
};
o.rows[0].elements[0]; // logs "Object { distance: {â¦}, duration: {â¦}, status: "OK" }"
请注意
o.rows[0].elements[0]
如果其中一个数组为空,将引发错误。有些库在这种情况下会优雅地失败: