php - 从文件获取内容获取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)" } }
如何获取包含对象的数组?尝试了很多事情都没有结果。
解决方案
问题是你正在使用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
。
推荐阅读
- google-apps-script - 使用 Google App Scripts 在电子邮件中附加 Google Docs 文件
- javascript - 添加验证中间件时,表达静态路由不起作用
- ios - Mapbox 使用 ionic livereload 工作,但在 Xcode 中运行时不起作用
- javascript - THREE.js 停止摄像头并在路径末端给予用户控制权
- flutter - 自动验证文本输入颤振的初始值
- ethereum - 有没有一种安全的方法可以将 API 密钥传递给 Chainlink 中的预言机?
- mediawiki - 如何使用 PluggableAuth - Mediawiki 接收 POST 数据?
- python - 如何为 Pytesseract 处理这个图像?
- node.js - 保存时对象的 Mongoose 数组为空
- python - Python Asyncio:GET 调用在 FPL API 端点上返回空响应