首页 > 解决方案 > 从文件获取内容获取json字符串时获取对象字符串(php)

问题描述

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

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

这是内容(不是全部):

字符串(3252)“{“种类”:“youtube#videoListResponse”,“etag”:“\”XI7nbFXulYBIpL0ayR_gDh3eu1k/wo842EqE4etxOSsLdlZIaoKM8M0\“”,“pageInfo”:{“totalResults”:1,“resultsPerPage”:1},“项目": [ { "kind": "youtube#video", "etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/HNnm6NCNPTGVCQmzWqpMBzSi9S8\"",
"id": "Mpxr7Rc0ycQ", "snippet": { "publishedAt": "2018-07- 18T11:29:51.000Z", "channelId": "UC8ezvxw6dD3dF5Xu9Aw7icA", "title": "安全摄像机和闭路电视 #2 捕获的 5 件奇怪的事情", "description": "description", "thumbnails":{“默认”:{“url”:“ https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg ”,“宽度”:120,“高度”:90},“中”:{“网址“:”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 ", "宽度": 640, "高度": 480 }, ......

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

数组(4){[“种类”]=>字符串(25)“youtube#videoListResponse”
[“etag”]=>字符串(57)“”XI7nbFXulYBIpL0ayR_gDh3eu1k/PPyttIkZuikajCWylsl_R32g_pE“”
[“pageInfo”]=>数组(2) { ["totalResults"]=> int(1) ["resultsPerPage"]=> int(1) } ["items"]=> array(1) { [0]=> string(8) "Array(4) " } }

特别是这部分很奇怪:

["items"]=> 数组(1) { [0]=> 字符串(8) "数组(4)" } }

如何获取包含对象的数组?尝试了很多事情都没有结果。

标签: phpjsonyoutube

解决方案


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

从 php 文档:

当 TRUE 时,返回的对象将被转换为关联数组。

所以,只是不要使用 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


推荐阅读