如何使用PHP以这种json格式获取描述?

时间:2017-10-17 08:07:18

标签: php json

如何使用php以这种json格式获取描述?

这是json数据

{
 "items": [
  {
   "id": "DHHlQxea3Pg",
   "snippet": {
    "description": "Private Jet trip to the Rocky Mountains 1080p"
   }
  }
 ]
}

这是我的代码

$curl_json_video_description = curl_init('some url');
curl_setopt($curl_json_video_description, CURLOPT_RETURNTRANSFER, true);
$json_video_description = curl_exec($curl_json_video_description);
$result_json_video_description = json_decode($json_video_description);
$video_description = $result_json_video_description->items->snippet->description;
echo $video_description;

当我测试我的代码时,它没有回应任何东西,我怎么能在json中获取描述?

2 个答案:

答案 0 :(得分:1)

您缺少0项密钥,因为items是项目数组。

使用$result_json_video_description->items[0]->snippet->description

OR 将其解码为数组json_decode($json, true),然后:

$result_json_video_description = json_decode();

$result_json_video_description['items'][0]['snippet']['description']

答案 1 :(得分:1)

此代码适用于我:)

$content = file_get_contents('url');
$jsonize = json_decode($content, true);

echo $jsonize['items'][0]['snippet']['description'];