从文件获取内容(php)获取json字符串时获取对象字符串

Get string of objects when get json string from file get contents (php)

我有以下代码从 YouTube 获取 json 字符串:

$dataSnippet = file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet&id=jZT82WmOTkw&key=[api-key]');

这是内容(不是全部):

string(3252) "{ "kind": "youtube#videoListResponse", "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 1 }, "items": [ { "kind": "youtube#video", "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8\"",
"id": "Mpxr7Rc0ycQ", "snippet": { "publishedAt": "2018-07-18T11:29:51.000Z", "channelId": "UC8ezvxw6dD3dF5Xu9Aw7icA", "title": "5 Weird Things Caught on Security Cameras & CCTV #2", "description": "description", "thumbnails": { "default": { "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg", "width": 120, "height": 90 }, "medium": { "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg", "width": 320, "height": 180 }, "high": { "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg", "width": 480, "height": 360 }, "standard": { "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg", "width": 640, "height": 480 }, ......

但是当我在它上面放一个 json_decode (assoc: true) 时,我得到这个:

array(4) { ["kind"]=> string(25) "youtube#videoListResponse"
["etag"]=> string(57) ""XI7nbFXulYBIpL0ayR_gDh3eu1k/PPyttIkZuikajCWylsl_R32g_pE""
["pageInfo"]=> array(2) { ["totalResults"]=> int(1) ["resultsPerPage"]=> int(1) } ["items"]=> array(1) { [0]=> string(8) "Array(4)" } }

尤其是这一段很奇怪:

["items"]=> array(1) { [0]=> string(8) "Array(4)" } }

如何获得包含对象的数组?试了很多东西都没有结果。

问题是您正在使用 json_decode($val, assoc: true)

来自 php 文档:

When TRUE, returned objects will be converted into associative arrays.

所以,不要使用 true,它应该按照你想要的方式处理事情。


<?php

$json = '{
    "kind": "youtube#videoListResponse",
    "etag": "XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0",
    "pageInfo": {
        "totalResults": 1,
        "resultsPerPage": 1
    },
    "items": [
        {
            "kind": "youtube#video",
            "etag": "XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8",
            "id": "Mpxr7Rc0ycQ",
            "snippet": {
                "publishedAt": "2018-07-18T11:29:51.000Z",
                "channelId": "UC8ezvxw6dD3dF5Xu9Aw7icA",
                "title": "5 Weird Things Caught on Security Cameras & CCTV #2",
                "description": "description",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg",
                        "width": 120,
                        "height": 90
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg",
                        "width": 320,
                        "height": 180
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg",
                        "width": 480,
                        "height": 360
                    },
                    "standard": {
                        "url": "https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg",
                        "width": 640,
                        "height": 480
                    }
                }
            }
        }
    ]
}';

$val = json_decode($json);
print_r($val);

转换为:

stdClass Object
(
    [kind] => youtube#videoListResponse
    [etag] => XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0
    [pageInfo] => stdClass Object
        (
            [totalResults] => 1
            [resultsPerPage] => 1
        )

    [items] => Array
        (
            [0] => stdClass Object
                (
                    [kind] => youtube#video
                    [etag] => XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8
                    [id] => Mpxr7Rc0ycQ
                    [snippet] => stdClass Object
                        (
                            [publishedAt] => 2018-07-18T11:29:51.000Z
                            [channelId] => UC8ezvxw6dD3dF5Xu9Aw7icA
                            [title] => 5 Weird Things Caught on Security Cameras & CCTV #2
                            [description] => description
                            [thumbnails] => stdClass Object
                                (
                                    [default] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )

                                    [medium] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )

                                    [high] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )

                                    [standard] => stdClass Object
                                        (
                                            [url] => https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg
                                            [width] => 640
                                            [height] => 480
                                        )

                                )

                        )

                )

        )

)

stdClass 是 PHP 的通用 object.

的内部等价物