如何使用PHP显示Json数据?

How to display Json data using PHP?

我有一个 json 文件,我想在 PHP 文件中显示它的数据。我尝试使用下面的代码,但它不起作用。

$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output, true);


foreach($json->data->items->thumbnail as $day) {

   echo $day->sqDefault;
   echo $day->hqDefault;
}

我的 json 文件是

{"apiVersion":"2.1","data":{"id":"PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL","author":"Tutorial top","title":"دورة سيو للمبتدئين [ali baba]","description":"","thumbnail":{"sqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg","hqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg"},"content":{"5":"http://www.youtube.com/p/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL"},"totalItems":31,"startIndex":1,"itemsPerPage":1,"items":[{"id":"PLH_kL5FgJPmdcYTGqaMXFsVJJ-pbR_YyiC1Y73R1tfYY","position":1,"author":"Tutorial top","video":{"id":"Kw8m5S2OhPs","uploaded":"2013-11-10T16:04:28.000Z","updated":"2014-02-18T17:59:36.000Z","uploader":"vkd-nAV91SLMCYNBKGJmsA","category":"People","title":"Beginners SEO Tutorial Course   Intro","description":"","thumbnail":
{"sqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg","hqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?
v=Kw8m5S2OhPs&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=Kw8m5S2OhPs"},"content":{"5":"https://www.youtube.com/v/Kw8m5S2OhPs?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-
4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":413,"aspectRatio":"widescreen","rating":5.0,"likeCount":"1","ratingCount":1,"viewCount":283,"favoriteCount":0,"commentCount":0,"accessControl":
{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed",
"autoPlay":"allowed","syndicate":"allowed"}}}]}}

我想要这样的输出

sqDefault  |   hqDefault

https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg,|https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg

首先,如果您想将 JSON 结构作为对象(而不是数组)进行访问,请删除 json_decode() 的第二个参数,否则您最终会得到一个数组$json。查看 first example 以了解差异。
然后你应该遍历 items 数组。它的每一个元素都有一个video属性,里面有你想要的thumbnail属性

$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output);
foreach ($json->data->items as $item) {
    echo $item->video->thumbnail->sqDefault;
    echo $item->video->thumbnail->hqDefault;
}

应该完成这项工作。你可以 see it in action.

为了清楚地了解结构,可以从众多在线可用的 JSON 观众中找到一个可以帮助您 ;-)

尝试从 json_decode()

中删除 true

经过多次试验,效果很好。

我已经在我的本地系统中进行了测试。

代码如下:--

<?php
$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output);
error_reporting(0);
foreach($json->data->thumbnail as $image) {
   echo $image.",|";
}

?>

输出:-

https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg,|https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg

希望对您有所帮助

看到我删除了 json_decode() 中的第二个参数,然后用 $json->data->items[0]->video->thumbnail 进行迭代,最后我用 rtrim()[=16 删除了最后一个 | =]

你可以使用这个例子:

<?php

$json_output = '{"apiVersion":"2.1","data":{"id":"PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL","author":"Tutorial top","title":"دورة سيو للمبتدئين [ali baba]","description":"","thumbnail":{"sqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg","hqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg"},"content":{"5":"http://www.youtube.com/p/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL"},"totalItems":31,"startIndex":1,"itemsPerPage":1,"items":[{"id":"PLH_kL5FgJPmdcYTGqaMXFsVJJ-pbR_YyiC1Y73R1tfYY","position":1,"author":"Tutorial top","video":{"id":"Kw8m5S2OhPs","uploaded":"2013-11-10T16:04:28.000Z","updated":"2014-02-18T17:59:36.000Z","uploader":"vkd-nAV91SLMCYNBKGJmsA","category":"People","title":"Beginners SEO Tutorial Course   Intro","description":"","thumbnail":
{"sqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg","hqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?
v=Kw8m5S2OhPs&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=Kw8m5S2OhPs"},"content":{"5":"https://www.youtube.com/v/Kw8m5S2OhPs?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-
4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":413,"aspectRatio":"widescreen","rating":5.0,"likeCount":"1","ratingCount":1,"viewCount":283,"favoriteCount":0,"commentCount":0,"accessControl":
{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed",
    "autoPlay":"allowed","syndicate":"allowed"}}}]}}';
 /*
  * If you want use the URL remove the comments
 */   
//$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");

    $json = json_decode($json_output);
    $string = '';

    foreach($json->data->items[0]->video->thumbnail as $day) {


  $string .= $day . "|";
}

echo rtrim($string, '|');

输出:

https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg|https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg