Teleport API:访问JSON对象属性



我正在学习一些关于Javascript中API调用的事情。为此,我调用了teleport api。

从那里我得到一个Json对象。现在我要访问json对象中的链接下面。

_embedded
city:search-results     [25]
0
_links
city:item
href:   https://api.teleport.org/api/cities/geonameid:2661552/

我在搜索结果"城市"键,因为我不能使用";

那么,我怎么能得到href值?

我希望这是可以理解的,我很抱歉我的解释不好。谢谢你的帮助!

我看到JSON是如何返回的。对于本例,使用括号表示city:search-results

_embedded["city:search-results"][0]

访问href属性:

_embedded["city:search-results"][0]["_links"]["city:item"]["href"]

示例代码(包含2个city:search-results对象):

var _embedded = {
"city:search-results": [{
"_links": {
"city:item": {
"href": "https://api.teleport.org/api/cities/geonameid:1796236/"
}
},
"matching_alternate_names": [],
"matching_full_name": "Shanghai, Shanghai, China"
},
{
"_links": {
"city:item": {
"href": "https://api.teleport.org/api/cities/geonameid:745044/"
}
},
"matching_alternate_names": [],
"matching_full_name": "Istanbul, Istanbul, Turkey"
}
]
};
console.log(_embedded["city:search-results"][0]["_links"]["city:item"]["href"]);