php decode JSON不能在数组中获取值


php decode JSON can't get a value inside an array

我试图从以下JSON中获取thumbnail_url

    {
  "uuid": "00012710-4b65-0131-ffe6-22000a499ea4",
  "camera_uuid": "98373a20-79ee-0130-3e42-1231390fcc11",
  "created_at": "2013-12-20T05:25:02.000Z",
  "percent_complete": 100,
  "short_code": "KKtB4Q",
  "metadata": {
  },
  "state": "published",
  "recorded_from": "http://singwho.com",
  "publish_type": null,
  "formats": [
    {
      "name": "720p",
      "width": 1280,
      "height": 720,
      "video_url": "https://cameratag.com/videos/00012710-4b65-0131-ffe6-22000a499ea4/720p/mp4.mp4",
      "flv_url": null,
      "flv_stream": null,
      "mp4_url": "https://cameratag.com/videos/00012710-4b65-0131-ffe6-22000a499ea4/720p/mp4.mp4",
      "mp4_stream": "rtmp://sa0xpxq1vz1ri.cloudfront.net/cfx/st&mp4:98373a20-79ee-0130-3e42-1231390fcc11/00012710-4b65-0131-ffe6-22000a499ea4_720p.mp4?Expires=1436913071&Signature=NppwMddejKbM2JMYrjsUUC5TJN0YfbgOox6sBKwO1YcftAaspf25ByG8drEG4zM-pTD6mST71YtBb3pQ5JzhHM33B6JQv0BsZvjGHarA7kVq9b6GG27wY5N5F6Sy79l5GNO3k9-Sh4MRy6fABZEDQUxd8TZ1HD7Usj2FlPYXxWXZmWQlo~43YKRnium7dcEzh-RRXbXfNQarfz1ju~OXI4J9ug1DRmHVtqV0F32cEDdCSCVy5Tyokf7IgO5SXATkuIkRtt52TdInFXmWmLbGfopDtKgua8NZXPaDbK35ra7AX2DuQE3iKTeX5oCWgVCPCAUz1PenLtK8rOYhTyYgJg__&Key-Pair-Id=APKAIYEVFIMHKY7YWGKA",
      "webm_url": "https://cameratag.com/videos/00012710-4b65-0131-ffe6-22000a499ea4/720p/webm.webm",
      "webm_stream": "rtmp://sa0xpxq1vz1ri.cloudfront.net/cfx/st&webm:98373a20-79ee-0130-3e42-1231390fcc11/00012710-4b65-0131-ffe6-22000a499ea4_720p.webm?Expires=1436913071&Signature=Qc-gjpwSHp3QmXzoiLuAMy11ReUHyLvRuuaszBYRT~GEzp~wl-TTZ-sGqo-XBlJlxH-54LmzXPryygS8ZCwdQQs0~5le69YXlL8RONl7kaLwmLSKlSE0PJlGzJoUW8kqO1mZxIfvrcmpYPdpCukm5J6eTv2U0rWCzAGfAeSiT7kUUc-9uGxLjjeOLIXVKebyYYhjT3wC-Gp5jd4ODCq3JB-IAWpOCOcXxF7oCuF-ag5WznaAeasW200M4yuYHvuDmu~dz~r52NcykeldzQ9Wq4laRuxaLRYaGZpB3y7og31RoWo75bomoT2vOO2rO-4~pz1tcfoYsg05T14er62KOA__&Key-Pair-Id=APKAIYEVFIMHKY7YWGKA",
      "thumbnail_url": "https://cameratag.com/videos/00012710-4b65-0131-ffe6-22000a499ea4/720p/thumb.png",
      "small_thumbnail_url": "https://cameratag.com/videos/00012710-4b65-0131-ffe6-22000a499ea4/720p/small_thumb.png",
      "state": "COMPLETED",
      "completed_at": "2013-12-20T05:30:12.000Z",
      "length": 195
    }
  ],
  "plays": 0
}

这就是如何解码JSON,我能够得到'uuid'

$raw = file_get_contents('php://input');
        $json = json_decode($raw, true);

I tried with

foreach ($json->formats as $format) {
        $url = $format["thumbnail_url"];
    }

$url = $json->formats[0]->thumbnail_url

$url = $json['formats']['thumbnail_url']

,但我仍然无法得到正确的值。我错过什么了吗?

你刚刚有了它的$url = $json['formats'][0]['thumbnail_url']

$json是一个嵌套数组。$json['formats']是对象的array,要获得第一个对象,使用$json['formats'][0]

你可以很容易地看到$jsonvar_export($json)print_r($json)