如何从此JSON数组中提取trackName?

时间:2017-07-12 20:44:00

标签: php arrays json

我有这个JSON数组(从iTunes返回),我希望从中提取trackName,artistName和previewURL。我已经尝试了各种foreach次迭代,但我似乎无法正常工作。

JSON数组:

{
  "resultCount": 5,
  "results": [
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180917,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "Born This Way",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "Born This Way",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4",
      "previewUrl": "http:\/\/a463.phobos.apple.com\/us\/r30\/Music1\/v4\/d5\/3c\/07\/d53c0773-ff35-1fe5-9be2-815038285c08\/mzaf_4465142801402798156.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2013-01-01T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 2,
      "trackTimeMillis": 260258,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902096524,
      "trackId": 902096558,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way",
      "trackName": "Born This Way",
      "collectionCensoredName": "Born This Way",
      "trackCensoredName": "Born This Way",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4",
      "previewUrl": "http:\/\/a459.phobos.apple.com\/us\/r30\/Music1\/v4\/c7\/2a\/83\/c72a8341-88b7-c65d-09dc-c333de92bb0c\/mzaf_4381737056492150878.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/100x100bb.jpg",
      "collectionPrice": 7.99,
      "trackPrice": 1.29,
      "releaseDate": "2011-02-11T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 14,
      "trackNumber": 2,
      "trackTimeMillis": 260258,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180935,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "The Edge of Glory",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "The Edge of Glory",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4",
      "previewUrl": "http:\/\/a642.phobos.apple.com\/us\/r30\/Music4\/v4\/bb\/93\/f6\/bb93f6dd-b09f-6378-9097-e41fab0a50e7\/mzaf_2685820553066586847.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2013-01-01T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 17,
      "trackTimeMillis": 321545,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 902180909,
      "trackId": 902180934,
      "artistName": "Lady Gaga",
      "collectionName": "Born This Way (Bonus Track Version)",
      "trackName": "Yo\u00fc and I",
      "collectionCensoredName": "Born This Way (Bonus Track Version)",
      "trackCensoredName": "Yo\u00fc and I",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4",
      "previewUrl": "http:\/\/a37.phobos.apple.com\/us\/r30\/Music1\/v4\/b3\/d0\/51\/b3d05174-db69-be68-272b-1b1dd35360d9\/mzaf_7395703188902498297.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg",
      "collectionPrice": 10.99,
      "trackPrice": 1.29,
      "releaseDate": "2011-05-23T07:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 22,
      "trackNumber": 16,
      "trackTimeMillis": 307364,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Pop",
      "isStreamable": true
    },
    {
      "wrapperType": "track",
      "kind": "song",
      "artistId": 277293880,
      "collectionId": 716415763,
      "trackId": 716415953,
      "artistName": "Lady Gaga",
      "collectionName": "NOW That's What I Call a Workout",
      "trackName": "Born This Way (Bimbo Jones Club Remix)",
      "collectionCensoredName": "NOW That's What I Call a Workout",
      "trackCensoredName": "Born This Way (Bimbo Jones Club Remix)",
      "collectionArtistId": 4035426,
      "collectionArtistName": "Various Artists",
      "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4",
      "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4",
      "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4",
      "previewUrl": "http:\/\/a294.phobos.apple.com\/us\/r30\/Music3\/v4\/25\/47\/e8\/2547e80d-c74c-7cf4-7905-041c7f7ec259\/mzaf_1883593890824568199.plus.aac.p.m4a",
      "artworkUrl30": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/30x30bb.jpg",
      "artworkUrl60": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/60x60bb.jpg",
      "artworkUrl100": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/100x100bb.jpg",
      "collectionPrice": 11.99,
      "trackPrice": -1,
      "releaseDate": "2012-12-18T08:00:00Z",
      "collectionExplicitness": "notExplicit",
      "trackExplicitness": "notExplicit",
      "discCount": 1,
      "discNumber": 1,
      "trackCount": 18,
      "trackNumber": 1,
      "trackTimeMillis": 284453,
      "country": "USA",
      "currency": "USD",
      "primaryGenreName": "Fitness & Workout",
      "isStreamable": false
    }
  ]
}

例如,我尝试了以下方面的内容:

foreach($obj['results'] as $row => $value) {
        foreach($value['trackName'] as $type => $output) {
            echo $output . "/n";
        }
    }

其中$ obj是json_decode的结果。我认为我的大脑今天只是炒了,因为我觉得它并不那么难,但我们就是这样。

0 个答案:

没有答案