Home>

I got the API of itune using superagent.

{
 "resultCount": 1,
 "results": [
{"wrapperType": "track", "kind": "song", "artistId": 635154255, "collectionId": 1376403610, "trackId": 1376403702, "artistName": "aiko", "collectionName": "straw- "EP", "trackName": "straw", "collectionCensoredName": "straw-EP", "trackCensoredName": "straw", "artistViewUrl": "https://itunes.apple.com/jp/artist/aiko/ 635154255? Uo = 4 "," collectionViewUrl ":" https://itunes.apple.com/album/%E3%82%B9%E3%83%88%E3%83%AD%E3%83%BC/1376403610? I = 1376403702&uo = 4 "," trackViewUrl ":" https://itunes.apple.com/album/%E3%82%B9%E3%83%88%E3%83%AD% E3% 83% BC/1376403610? I = 1376403702&uo = 4 ",
"previewUrl": "https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview118/v4/e0/de/a8/e0dea8ee-51c7-6b41-c89c-a2e8bf2b0a3c/mzaf_9129827488191895000 .plus.aac.p.m4a "," artworkUrl30 ":" https://is1-ssl.mzstatic.com/image/thumb/Music128/v4/27/a4/09/27a409f4-5785-dc04-9f4a-a85a56eb640f /source/30x30bb.jpg "," artworkUrl60 ":" https://is1-ssl.mzstatic.com/image/thumb/Music128/v4/27/a4/09/27a409f4-5785-dc04-9f4a-a85a56eb640f/source /60x60bb.jpg "," artworkUrl100 ":" https://is1-ssl.mzstatic.com/image/thumb/Music128/v4/27/a4/09/27a409f4-5785-dc04-9f4a-a85a56eb640f/source/100x100bb .jpg "," collectionPrice ":-1.00," trackPrice ": 250.00," releaseDate ":" 2018-05-02T07: 00: 00Z "," collectionExplicitness ":" notExplicit "," trackExplicitness ":" notExplicit "," discCount ": 1," discNumber ": 1," trackCount ": 4," trackNumber ": 1," trackTimeMillis ": 295938," country ":" JPN "," currency ":" JPY "," primaryGenreName ":" J-Pop "," isStreamable ": false}]` ``

I want to operate this Json with Javascript.

artistSearch (e) {
let url = 'https://itunes.apple.com/search?term=' +
this.state.artist + '&media = music&country = jp&lang = en_jp'
request
.get (url)
.end ((err, res) =>{
if (err) {
return
}
this.setState ({
songs: String (res.text)
})
})
}


The entire Json file can be read with res.text, but details cannot be taken.
The data I want to get is trackName and previewUrl of the above Json file.

  • Answer # 1

    Is the JSON string uploaded to the question wrong? Is the last one below?

    Incorrect: ..."isStreamable": false}] `` `

    Correct: ..."isStreamable": false}]}

    If you fix it, you should be able to get it as follows.

    var jsonString2 = '{"resultCount": 1, "results": [{"wrapperType": "track" ... "isStreamable": false}]}';
    var data2 = JSON.parse (jsonString2);
    var trackName = data2.results [0] .trackName;
    var previewUrl = data2.results [0] .previewUrl;