如何解码 youtube api v3 json 响应

Posted

技术标签:

【中文标题】如何解码 youtube api v3 json 响应【英文标题】:how to decode youtube api v3 json response 【发布时间】:2015-07-21 22:21:21 【问题描述】:

这是我的 youtube json 响应

    query results:Array
(
    [kind] => youtube#searchListResponse
    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_RyGfO1dwDRqY9aMf412I8xvZgw"
    [nextPageToken] => CAoQAA
    [pageInfo] => Array
        (
            [totalResults] => 1000000
            [resultsPerPage] => 10
        )

    [items] => Array
        (
            [0] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/vJBGevzCpIbHOcHu7AAc4WsQ92E"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => eHfMfHyiWYA
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2013-11-04T16:09:20.000Z
                            [channelId] => UCSvOdBJgMnTYsK-cZIGZSYQ
                            [title] => Ann Arbor - a test bed for driverless cars?
                            [description] => University of Michigan researchers work to make Ann Arbor the first American city with a shared fleet of connected and driverless vehicles by 2021. The cars of ...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/eHfMfHyiWYA/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/eHfMfHyiWYA/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/eHfMfHyiWYA/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => michiganengineering
                            [liveBroadcastContent] => none
                        )

                )

            [1] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9Mm7w3esGwaHCTasG_67T33aULE"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => PRvGaCnfV-8
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2009-01-26T16:50:35.000Z
                            [channelId] => UCPkN7EcOJlQAzCLxaZSn_5g
                            [title] => Timelapse Cars
                            [description] => 
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/PRvGaCnfV-8/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/PRvGaCnfV-8/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/PRvGaCnfV-8/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => frehleycomet
                            [liveBroadcastContent] => none
                        )

                )

            [2] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/9_WnlpVsvx2WLKqdgxGXI3CRvBE"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => U60TfgpHqSc
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2013-07-01T23:23:59.000Z
                            [channelId] => UCULJH9kW-UdTBCDu27P0BoA
                            [title] => Are cars killing the Bicycle Kingdom?
                            [description] => Bike advocacy in the Bicycle Kingdom? China was once known as the Bicycle Kingdom but with the growth of the automobile, the nature of biking in Beijing is ...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/U60TfgpHqSc/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/U60TfgpHqSc/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/U60TfgpHqSc/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => llukitsch
                            [liveBroadcastContent] => none
                        )

                )

            [3] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/_2N5Lkva8p_pbJqLf5K61gNPHL0"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => Xg5-oxTSM04
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2014-02-13T17:42:31.000Z
                            [channelId] => UC-Gu_VkJmS1FGxsrSddKvfg
                            [title] => Market America's 2014 World Conference: FreeBee Cars
                            [description] => Market America's 2014 World Conference in Miami featured FreeBee cars showcasing MA brands like Motives® Cosmetics and DNA Miracles™.
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/Xg5-oxTSM04/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/Xg5-oxTSM04/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/Xg5-oxTSM04/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => MarketAmerica
                            [liveBroadcastContent] => none
                        )

                )

            [4] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/VXZohXlA5kHPvrJ-rxpdEjUsaFY"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => yrcq5awviik
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2011-06-01T23:31:01.000Z
                            [channelId] => UC1URrXTrZ68pZZ4YDmT4WVQ
                            [title] => Ford's connected cars get street-smart
                            [description] => At AT&T Park in San Francisco, SmartPlanet correspondent Sumi Das talks to Ford's Mike Shulman about the company's research into intelligent vehicles.
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/yrcq5awviik/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/yrcq5awviik/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/yrcq5awviik/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => SmartPlanetCBS
                            [liveBroadcastContent] => none
                        )

                )

            [5] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/FWmwcjir_8QSK6eq5D5gsY-lWXQ"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => bFV7WU6UHaQ
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2014-05-14T10:59:48.000Z
                            [channelId] => UCI1H2wxkvshpCcUNNOEc4Gw
                            [title] => Google's Driverless Cars Take to the Streets
                            [description] => Google's self-driving car technology is not likely to be available for several more years. But the company is already beginning the job of making the public ...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/bFV7WU6UHaQ/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => IBTimesUK
                            [liveBroadcastContent] => none
                        )

                )

            [6] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/t7dSXvQ-37jK0DFYHxk4iP1o3-s"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => ifzSVGEHxMU
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2012-10-22T13:45:39.000Z
                            [channelId] => UC6s5vC0tCuStbt4d5dfhmjA
                            [title] => Project C.A.R.S: BMW Z4
                            [description] => Scott tries out the newest car to be added, the BMW Z4. Those of you waiting for Tekkit, it will return soon! Hound is currently in the process of moving! Pl...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/ifzSVGEHxMU/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/ifzSVGEHxMU/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/ifzSVGEHxMU/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => scott2k44
                            [liveBroadcastContent] => none
                        )

                )

            [7] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/RW6QW1hjACRmSo6Za1BzsP1gbSk"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => YZggnfolreA
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2015-05-08T21:32:35.000Z
                            [channelId] => UC87geOOB2rCWkJFtUUeEBsw
                            [title] => Project CARS vs. DriveClub | Graphics, Sound, Rain & Weather Effects Gameplay Comparison (PS4)
                            [description] => Comparing the biggest racing games available on the PS4, DriveClub (PS4 exclusive) and Project CARS (also available on PC and Xbox One). Both games are ...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/YZggnfolreA/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/YZggnfolreA/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/YZggnfolreA/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => VideoGamesNL
                            [liveBroadcastContent] => none
                        )

                )

            [8] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/OIAM0bSsr9oHqPwPPg3msZUqeMU"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => hJxFid8A0Xw
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2015-04-02T16:00:05.000Z
                            [channelId] => UCNjakstwb629k6IE9BualRw
                            [title] => Comedians In Cars Getting Coffee: Single Shot - Analogy Lad
                            [description] => Watch it on Crackle: [http://bit.ly/SingleShot] Jerry's special guests are Todd Barry, George Wallace, Sarah Silverman, Fred Armisen, Kevin Hart and Jimmy ...
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/hJxFid8A0Xw/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/hJxFid8A0Xw/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/hJxFid8A0Xw/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => Crackle
                            [liveBroadcastContent] => none
                        )

                )

            [9] => Array
                (
                    [kind] => youtube#searchResult
                    [etag] => "tbWC5XrSXxe1WOAx6MK9z4hHSU8/Q945pOROs0oCGIOcFFq66OWZZdw"
                    [id] => Array
                        (
                            [kind] => youtube#video
                            [videoId] => I00AXDv8ncY
                        )

                    [snippet] => Array
                        (
                            [publishedAt] => 2015-05-07T07:03:27.000Z
                            [channelId] => UCyQCcFWSh7MLAhdq2a_2jKA
                            [title] => Project CARS Online #1 - Los gehts! - Formula A @Monza | Wheel/Controller
                            [description] => Project CARS ist ein Motorsport Multiplayer-Simulation Spiel, entwickelt und veröffentlicht vom britischen Video-Spiel-Entwickler Slightly Mad Studios.
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/I00AXDv8ncY/default.jpg
                                        )

                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/I00AXDv8ncY/mqdefault.jpg
                                        )

                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/I00AXDv8ncY/hqdefault.jpg
                                        )

                                )

                            [channelTitle] => 
                            [liveBroadcastContent] => none
                        )

                )

        )

)   

我想使用 foreach 循环从这个 json 响应中提取视频标题、缩略图、描述

我试过这个,但它给了我一个错误请帮助我解决这个问题。

我的php代码是

$link = 'https://www.googleapis.com/youtube/v3/search?order=rating&part=snippet&q=cars&maxResults=10&key=KEY';

$video = file_get_contents($link);
$video = json_decode($video, true); 
echo '<pre>';
print_r("query results:");
print_r($video);
'</pre>';

foreach ($video->items as $data )
    echo "$data->snippet->title";
    echo "<br>";

【问题讨论】:

【参考方案1】:

$video是array,不是对象,使用$video['items'],与$data等一样

【讨论】:

谢谢你能告诉我在json响应中访问标题的方式 echo $data['snippet']['title'];

以上是关于如何解码 youtube api v3 json 响应的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 python 从 youtube v3 api 修复 json?

如何通过 YouTube api v2/v3 (JSON) 获取已删除视频的信息

如何使用 Ajax 和 JSON 在 javascript 中使用 v3 URL API 获取 Youtube 视频标题

如何使用 JavaScript API V3 以 JSON 格式获取 youtube 播放列表的数据

使用 alt=json-in-script&callback 检索 info V3 youtube API

如何使用 YouTube API V3 获取视频观看次数?