如何在PHP中解析此JSON?

时间:2014-03-12 15:07:32

标签: php arrays json parsing

我试图将MediaUrl(仅表示URL)的值转换为PHP上带有JSON的字符串数组。我想忽略其他一切。这是从我调用的URL返回的内容。在PHP中我需要做什么才能将这些值放入数组中?

{
  "d": {
    "results": [
     {
       "__metadata":
       {
         "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='12 monkeys blu-ray'&$skip=0&$top=1",
         "type": "ImageResult"
       },
       "MediaUrl": "http://www.dvdlink.ca/images/Movies%20Covers/9779/47/1250724527_1250911533.jpg",
       "Thumbnail": {
         "__metadata": {
         "type": "Bing.Thumbnail"
       },
         "MediaUrl": "http://ts4.mm.bing.net/th?id=HN.607991009242185763&pid=15.1",
         "FileSize": "10424"
       }
     },
     {
       "__metadata": {
         "uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='12 monkeys blu-ray'&$skip=1&$top=1",
         "type": "ImageResult"
        },
        "MediaUrl": "http://www.bluray-dvd-film-shop.at/media/images/popup/12-Monkeys-bluray.jpg",
        "Thumbnail": {
          "__metadata": {
            "type": "Bing.Thumbnail"
          },
          "MediaUrl": "http://ts4.mm.bing.net/th?id=HN.608017534956014467&pid=15.1",
          "FileSize": "17060"
         }
        }
      ],
      "__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='12%20monkeys%20blu-ray'&$skip=50"
    }
  }

2 个答案:

答案 0 :(得分:1)

$result = array();
$array = json_decode($your_json_string, true);
    foreach ($array['d']['results'] as $entry)
        $result[] = $entry['MediaUrl'];

您需要的所有内容都不在$ result数组中,如果没有结果(缺少MediaUrl),则空($ result)将返回TRUE。

答案 1 :(得分:0)

你可以这样做

json_decode($data,true);

这将为您提供一个数组,您可以循环访问数组以获取数据。

$data = 'Your JSON string' ;

以下是手册http://in2.php.net/json_decode