Javascript:如何在JSON对象的数组中获取键值

时间:2016-02-01 12:31:04

标签: jquery json getjson jquery-callback

您好我正在构建一个Web应用程序,我使用itunes搜索API。 因此,当我搜索艺术家的特定曲目时,itunes会向我返回一个JSON,其中包含与我所放置的搜索词相关的一些信息。

JSON看起来像这样:

{
 "resultCount":1,
 "results": [
{"wrapperType":"track", "kind":"song", "artistId":271256, "collectionId":1025130620, "trackId":1025130621, "artistName":"Drake", "collectionName":"Hotline Bling - Single", "trackName":"Hotline Bling", "collectionCensoredName":"Hotline Bling - Single", "trackCensoredName":"Hotline Bling", "artistViewUrl":"https://itunes.apple.com/us/artist/drake/id271256?uo=4", "collectionViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "previewUrl":"http://a1586.phobos.apple.com/us/r1000/172/Music6/v4/f6/2f/76/f62f7681-0f4c-3a9f-9aeb-2098297652f7/mzaf_7372296458390444020.plus.aac.p.m4a", "artworkUrl30":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/30x30bb.jpg", "artworkUrl60":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/60x60bb.jpg", "artworkUrl100":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/100x100bb.jpg", "collectionPrice":1.29, "trackPrice":1.29, "releaseDate":"2015-07-31T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":1, "trackNumber":1, "trackTimeMillis":267024, "country":"USA", "currency":"USD", "primaryGenreName":"Hip-Hop/Rap", "radioStationUrl":"https://itunes.apple.com/station/idra.1025130621", "isStreamable":true}]
}

我如何获得关于" artistId"的具体价值?那是在结果数组里面吗?

我使用$ .getJSON调用,在回调中我无法获得此值。

在javascript的第54行查看此jsfiddle example

2 个答案:

答案 0 :(得分:4)

你不能得到这样的身份

 var arr={
 "resultCount":1,
 "results": [
{"wrapperType":"track", "kind":"song", "artistId":271256, "collectionId":1025130620, "trackId":1025130621, "artistName":"Drake", "collectionName":"Hotline Bling - Single", "trackName":"Hotline Bling", "collectionCensoredName":"Hotline Bling - Single", "trackCensoredName":"Hotline Bling", "artistViewUrl":"https://itunes.apple.com/us/artist/drake/id271256?uo=4", "collectionViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "trackViewUrl":"https://itunes.apple.com/us/album/hotline-bling/id1025130620?i=1025130621&uo=4", "previewUrl":"http://a1586.phobos.apple.com/us/r1000/172/Music6/v4/f6/2f/76/f62f7681-0f4c-3a9f-9aeb-2098297652f7/mzaf_7372296458390444020.plus.aac.p.m4a", "artworkUrl30":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/30x30bb.jpg", "artworkUrl60":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/60x60bb.jpg", "artworkUrl100":"http://is1.mzstatic.com/image/thumb/Music7/v4/2d/c5/31/2dc53130-44b8-8347-2f83-bce05ee8a649/source/100x100bb.jpg", "collectionPrice":1.29, "trackPrice":1.29, "releaseDate":"2015-07-31T07:00:00Z", "collectionExplicitness":"notExplicit", "trackExplicitness":"notExplicit", "discCount":1, "discNumber":1, "trackCount":1, "trackNumber":1, "trackTimeMillis":267024, "country":"USA", "currency":"USD", "primaryGenreName":"Hip-Hop/Rap", "radioStationUrl":"https://itunes.apple.com/station/idra.1025130621", "isStreamable":true}]
}
arr.results.forEach(function(i,j){console.log(i.artistId)})

答案 1 :(得分:1)

在您提供的小提琴中,您尝试通过编写data.results.artistId来访问artistId,但结果是一个数组,因此您必须访问其中一个索引的属性。所以像data.results[0].artistId这样的东西应该适合你。