javascript - javascript:在 JSON 响应中找不到数据
问题描述
我想使用 twitter 和 spotify API 在 twitter 上创建一个机器人。到目前为止,我已经有了一个好的开始,但是现在我遇到了一个我自己无法解决的问题:
我需要知道艺术家的名字和 Spotify 前 50 首歌曲中前 1 名的歌曲。所以我发送了一个 Spotify API 请求,我得到了这个 json 响应:
{
"href" : "https://api.spotify.com/v1/playlists/37i9dQZEVXbIPWwFssbupI/tracks?offset=0&limit=1",
"items" : [ {
"added_at" : "1970-01-01T00:00:00Z",
"added_by" : {
"external_urls" : {
"spotify" : "https://open.spotify.com/user/"
},
"href" : "https://api.spotify.com/v1/users/",
"id" : "",
"type" : "user",
"uri" : "spotify:user:"
},
"is_local" : false,
"primary_color" : null,
"track" : {
"album" : {
"album_type" : "single",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/7IlRNXHjoOCgEAWN5qYksg"
},
"href" : "https://api.spotify.com/v1/artists/7IlRNXHjoOCgEAWN5qYksg",
"id" : "7IlRNXHjoOCgEAWN5qYksg",
"name" : "Aya Nakamura",
"type" : "artist",
"uri" : "spotify:artist:7IlRNXHjoOCgEAWN5qYksg"
} ],
"available_markets" : [ "AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE",
"DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA" ],
"external_urls" : {
"spotify" : "https://open.spotify.com/album/43sqDUJkvHHK0ikoAN4JAS"
},
"href" : "https://api.spotify.com/v1/albums/43sqDUJkvHHK0ikoAN4JAS",
"id" : "43sqDUJkvHHK0ikoAN4JAS",
"images" : [ {
"height" : 640,
"url" : "https://i.scdn.co/image/ab67616d0000b273e2e7c5322770c06bdfead61f",
"width" : 640
}, {
"height" : 300,
"url" : "https://i.scdn.co/image/ab67616d00001e02e2e7c5322770c06bdfead61f",
"width" : 300
}, {
"height" : 64,
"url" : "https://i.scdn.co/image/ab67616d00004851e2e7c5322770c06bdfead61f",
"width" : 64
} ],
"name" : "Jolie nana",
"release_date" : "2020-07-17",
"release_date_precision" : "day",
"total_tracks" : 1,
"type" : "album",
"uri" : "spotify:album:43sqDUJkvHHK0ikoAN4JAS"
},
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/7IlRNXHjoOCgEAWN5qYksg"
},
"href" : "https://api.spotify.com/v1/artists/7IlRNXHjoOCgEAWN5qYksg",
"id" : "7IlRNXHjoOCgEAWN5qYksg",
"name" : "Aya Nakamura",
"type" : "artist",
"uri" : "spotify:artist:7IlRNXHjoOCgEAWN5qYksg"
} ],
"available_markets" : [ "AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO",
"RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA" ],
"disc_number" : 1,
"duration_ms" : 147076,
"episode" : false,
"explicit" : false,
"external_ids" : {
"isrc" : "FRZ042000666"
},
"external_urls" : {
"spotify" : "https://open.spotify.com/track/34FbkFgAbv4ffvhL2XQMVm"
},
"href" : "https://api.spotify.com/v1/tracks/34FbkFgAbv4ffvhL2XQMVm",
"id" : "34FbkFgAbv4ffvhL2XQMVm",
"is_local" : false,
"name" : "Jolie nana",
"popularity" : 83,
"preview_url" : "https://p.scdn.co/mp3-preview/f38309ea40d6b20b4a7a6b72209b503342b1b147?cid=8f22024a4f144cd7b965b9db6e124e94",
"track" : true,
"track_number" : 1,
"type" : "track",
"uri" : "spotify:track:34FbkFgAbv4ffvhL2XQMVm"
},
"video_thumbnail" : {
"url" : null
}
} ],
"limit" : 1,
"next" : "https://api.spotify.com/v1/playlists/37i9dQZEVXbIPWwFssbupI/tracks?offset=1&limit=1",
"offset" : 0,
"previous" : null,
"total" : 50
}
我将该响应放入变量myJSON
中,并且我想创建一个仅包含的变量Aya Nakamura
和另一个仅包含的变量Jolie Nana
我已经尝试使用检索数据myJSON.items
,myJSON[items]
但是这两行都返回undefined
我希望我已经解释得很好。
感谢您的帮助,请原谅我的英语,因为我会说法语。
解决方案
您没有解析 json,因此您的数据类型不是您怀疑的对象,而是字符串。此外,您可以像解析字符串一样访问字符串的属性,但它们返回未定义的,而不是您想要的数据。这里有一些例子:
const data = `
{"foo": "bar"}
`
console.log('data:', typeof data, data)
const parsedData = JSON.parse(data)
// Notice how similar this looks to the other in the console.
// It is not always clear what the type is if you don't check it,
// since JSON strings look similar to JS objects
console.log('parsedData:', typeof parsedData, parsedData)
// accessing an item:
console.log('data.foo:', data.foo) // since this is a string, it is `undefined`
console.log('parsedData.foo:', parsedData.foo) // since this is a parsed object, it works
推荐阅读
- python - Conda&dlib: ImportError: DLL load failed while importing _dlib_pybind11: 动态链接库 (DLL) 初始化例程失败
- html - 使用 Raspberry Pi 从 aqi.json 文件更新 highcharts 问题
- java - 为什么@ManyToOne 中没有 mappedBy 属性
- flutter - 条件为假时如何删除列表视图项?
- artificial-intelligence - 人工智能中的事件是什么意思
- python - Python 重定向运算符文件名
- javascript - Change background-color with javascript, with same button no jquery or other
- java - 尝试在 IntelliJ 中运行 maven javafx-archetype-simple 时出错
- python - 调用时 Numba CUDA ctypes.ArgumentError
- c# - 如何使用 Xamarin.iOS 将广告内容分配给 stacklayout