所以我試圖解析一些Youtube API返回的JSON。我一直在理解如何解析PHP中的JSON,並且我似乎無法弄清楚。在PHP中用JSON解析括號外的數據
這是基本的API輸出:
{
"kind": "youtube#channelListResponse",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/mgiCAEvrnAFhKSCga80wVfAbUtc\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"uQc-MPTsstrHkQcRXL3IWLmeNsM/cndol6pHIKmkRCizokwycOOUr2E\"",
"id": "SWMb9NxQL9I6c",
"snippet": {
"title": "Koffee With Karan",
"description": "Catch Karan Johar chat up Bollywood, secrets revealed and stories told , that's how the world will remember Koffee with Karan",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/sh/554333074/showposter_thumb.jpg"
},
"medium": {
"url": "https://i.ytimg.com/sh/554333074/showposter.jpg"
},
"high": {
"url": "https://i.ytimg.com/sh/554333074/showposter_hq.jpg"
}
}
}
}
]
}
我想要得到的是在「片段」,我需要的標題,描述和縮略圖幾乎一切。我一直在試圖訪問它是這樣的:
$getjson = file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=snippet&id=SWMb9NxQL9I6c&key=mykey');
$data = json_decode($getjson,true);
echo $data['items'][0]['snippet'][0]['title'];
甚至試圖像
echo $data['items']->snippet->title;
事情我已經發現了大量的文章沒有解釋如何,但似乎工作或意義。首先,「項目」數據不在起始的{}括號內,我沒有真正能夠找到任何關於它的信息。
任何幫助,主要是關於這是如何工作的解釋是真棒。我只想了解訪問這些東西所需的語法。
關閉......片段是不是一個數組而是一個對象,所以你不['snippet'] [0] ['title']'just'['snippet'] ['title']' – Augwa