我有以下代码从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”,“摘要”:{ “ publishedAt”:“ 2018-07-18T11:29:51.000Z”, “ channelId”:“ UC8ezvxw6dD3dF5Xu9Aw7icA”, “ title”:“在安全摄像机和CCTV#2上发现了5件事”, “ description”:“ description”, “缩略图”:{ “默认”:{ “ url”:“ https://i.ytimg.com/vi/Mpxr7Rc0ycQ/default.jpg”, “宽度”:120, “身高”:90 }, “中”:{ “ url”:“ https://i.ytimg.com/vi/Mpxr7Rc0ycQ/mqdefault.jpg”, “宽度”:320, “身高”:180 }, “高”:{ “ url”:“ https://i.ytimg.com/vi/Mpxr7Rc0ycQ/hqdefault.jpg”, “宽度”:480, “身高”:360 }, “标准”:{ “ url”:“ https://i.ytimg.com/vi/Mpxr7Rc0ycQ/sddefault.jpg”, “宽度”:640, “身高”:480 },......
但是当我在上面放一个json_decode(关联:true)时,我得到了:
array(4){[“” kind“] =>字符串(25)” youtube#videoListResponse“
[“ etag”] =>字符串(57) “” XI7nbFXulYBIpL0ayR_gDh3eu1k / PPyttIkZuikajCWylsl_R32g_pE“”
[“ pageInfo”] => array(2){ [“ totalResults”] => 整数(1) [“ resultsPerPage”] => int(1)} [“ items”] => array(1){ [0] => string(8)“ Array(4)”}}
尤其是这部分很奇怪:
[“ items”] => array(1){ [0] => string(8)“ Array(4)”}}
如何获取包含对象的数组?尝试了很多事情而没有结果。
答案 0 :(得分:1)
问题是您正在使用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
的内在等同物。