解析JSON对象时出现问题 [英] Problems with parsing JSON objects

查看:82
本文介绍了解析JSON对象时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My website calls the Spotify Web API and gets all the public playlists for a given user. The response is originally in JSON but my code decodes it.

我想要代码做的下一件事,是仅显示对象 [external_urls] [name] [tracks] 响应中的每个项目.为此,我已经尝试过:

The next thing I want the code to do, is to display only the objects [external_urls], [name] and [tracks] for each item in the response. For that I've tried this:

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

...但是它不能正常工作.所有 [name] 对象(即一个字符串)可正确回显,但适用于所有 [external_urls] [tracks] (

...but it doesn't work as properly. All the [name]-objects (which is a string) are echoed correctly but for all [external_urls] and [tracks] (which are objects) it only says "Array" in the browser.

我想我应该为那些对象使用print_r(),但是我无法使其正常工作.

I guess I should be using print_r() for those objects but I can't get it to work.

有什么建议吗?

<?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
}

推荐答案

external_urlstracks都是对象.为了获取数据,您需要知道确切的属性或将它们强制转换为数组(array)并遍历新创建的数组以确保输出所有值.由于名称是复数形式,因此我假设您可以使用多个外部URL和多个轨道.因此,我将属性转换为数组并遍历它们:

Both external_urls and tracks are objects. In order to get the data, you either need to know the exact properties or cast them to an array (array) and traverse the newly created array to ensure you output all values. Since the names are pluralized I'm assuming you could have more than one external url and more than one track. For this reason, I've casted the properties to arrays and traversed through them:

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 />";
    } 
}

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

Alternatively, if you know that you will only have one url and one track and you know the property names, you can access the properties using object syntax object->key; The same example from above is rewritten below using object syntax:

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

这篇关于解析JSON对象时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆