解析JSON对象时出现问题

时间:2014-07-02 19:45:40

标签: php json parsing object spotify

我的网站调用Spotify Web API并获取给定用户的所有公共播放列表。响应最初是JSON,但我的代码对其进行了解码。

我希望代码做的下一件事就是只显示对象 [external_urls] [name] [tracks] 对于响应中的每个项目。为此,我试过这个:

foreach($response_2['items'] as $item) {
echo 'Link: ' . $item['external_urls'] . '<br />'; 
echo 'Name: ' . $item['name'] . '<br />';
echo 'Tracks: ' . $item['tracks'] . '<br />'; }

...但它没有正常工作。所有 [name] -objects(a string)都会正确回显,但所有 [external_urls] [曲目] (它们是objects)它只在浏览器中显示“数组”。

我想我应该对这些对象使用print_r(),但我无法让它工作。

有什么建议吗?

我的代码:

<?php

$url = 'https://accounts.spotify.com/api/token';
$method = 'POST';

$credentials = "hidden:hidden";

$headers = array(
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
        "Authorization: Basic " . base64_encode($credentials));
$data = 'grant_type=client_credentials';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_1 = curl_exec($ch);

curl_close($ch);

$response_1 = json_decode($response_1, true);

$token = $response_1['access_token'];

$headers_2 = array(
        "Accept: */*",
        "Content-Type: application/x-www-form-urlencoded",
        ('Authorization: Bearer ' . $token));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://api.spotify.com/v1/users/wizzler/playlists');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_2 = curl_exec($ch);

curl_close($ch);

$response_2 = json_decode($response_2, true);

?>

API响应:

{
    "href": "https://api.spotify.com/v1/users/wizzler/playlists",
    "items": [
        {
            "collaborative": false,
            "external_urls": {
                "spotify": "http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c"
            },
            "href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c",
            "id": "53Y8wT46QIMz5H4WQ8O22c",
            "name": "Wizzlers Big Playlist",
            "owner": {
                "external_urls": {
                    "spotify": "http://open.spotify.com/user/wizzler"
                },
                "href": "https://api.spotify.com/v1/users/wizzler",
                "id": "wizzler",
                "type": "user",
                "uri": "spotify:user:wizzler"
            },
            "public": true,
            "tracks": {
                "href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks",
                "total": 30
            },
            "type": "playlist",
            "uri": "spotify:user:wizzler:playlist:53Y8wT46QIMz5H4WQ8O22c"
        },
        {
            "collaborative": false,
            "external_urls": {
                "spotify": "http://open.spotify.com/user/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju"
            },
            "href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju",
            "id": "1AVZz0mBuGbCEoNRQdYQju",
            "name": "Another Playlist",
            "owner": {
                "external_urls": {
                    "spotify": "http://open.spotify.com/user/wizzlersmate"
                },
                "href": "https://api.spotify.com/v1/users/wizzlersmate",
                "id": "wizzlersmate",
                "type": "user",
                "uri": "spotify:user:wizzlersmate"
            },
            "public": true,
            "tracks": {
                "href": "https://api.spotify.com/v1/users/wizzlersmate/playlists/1AVZz0mBuGbCEoNRQdYQju/tracks",
                "total": 58
            },
            "type": "playlist",
            "uri": "spotify:user:wizzlersmate:playlist:1AVZz0mBuGbCEoNRQdYQju"...
        }
    ],
    "limit": 9,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 9
}

1 个答案:

答案 0 :(得分:1)

external_urlstracks都是对象。为了获取数据,您需要知道确切的属性或将它们转换为数组(array)并遍历新创建的数组以确保输出所有值。由于名称是多元化的,我假设您可以拥有多个外部网址和多个轨道。出于这个原因,我将属性转换为数组并遍历它们:

foreach($response_2['items'] as $item) {
    echo "LINKS <br />";
    $item['external_urls'] = (array)$item['external_urls'];
    foreach($item['external_urls'] as $key => $value) {
        echo ucfirst($key) . ": " . $value . "<br />";
    }
    echo "Name: " . $item['name'] . "<br />";
    echo "TRACKS <br />";
    $item['tracks'] = (array)$item['tracks'];
    foreach($item['tracks'] as $key => $value) {
        echo ucfirst($key) . ": " . $value . "<br />";
    } 
}

或者,如果您知道只有一个网址和一个跟踪并且您知道属性名称,则可以使用对象语法object->key;访问属性。使用对象语法在下面重写上面的相同示例:

foreach($response_2['items'] as $item) {
    echo "LINKS <br />";
    echo "Spotify: " . $item['external_urls']->spotify . "<br />";
    echo "Name: " . $item['name'] . "<br />";
    echo "TRACKS <br />";
    echo "Href: " . $item['tracks']->href . "<br />";
    echo "Total: " . $item['tracks']->total . "<br />";
}

第1项的示例输出:

LINKS
Spotify: http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c
Name: Wizzlers Big Playlist
TRACKS
Href: https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks
Total: 30