如何在PHP中从JSON获取变量?

时间:2011-10-21 05:08:00

标签: php json

我尝试了很多脚本,但都没有。

这是我的代码:

$contents = file_get_contents("http://video.foxnews.com/v/feed/video/1228085488001.js"); 
var_dump(json_decode($contents, true));

输出:

  

array(2){[“@attributes”] => array(1){[“version”] => string(3)“2.0”   } [“channel”] => array(2){[“title”] => array(0){} [“item”] =>   array(2){[“title”] =>字符串(49)“政府应该离开   止赎危机?“[”media-content“] => array(32){[”@attributes“] =>   array(1){[“url”] =>串(60)   “http://media2.foxnews.com/102011/ff_massi_102011_FNC_MED.mp4”}   [ “媒体播放器”] => array(1){[“@attributes”] => array(1){[“url”] =>   串(91)   “http://video.foxnews.com/assets/video-player.swf?video_id=1228085488001&d=video.foxnews.com”   }“[media-description”] => string(39)“房地产专家Bob Massi   声音关闭“[”media-thumbnail“] =>字符串(70)   “http://video.foxnews.com/thumbnails/102011/640/360/ff_massi_102011.jpg”   [ “媒体的关键字”] => array(0){} [“media-credit”] =>串(16)   “foxnews1-foxnews”[“mvn-assetUUID”] => string(13)“1228085488001”   [ “MVN-mavenId”] => array(0){} [“mvn-creationDate”] =>串(25)   “2011-10-20T08:38:49-04:00”[“mvn-airDate”] =>串(25)   “2011-10-20T08:38:49-04:00”[“mvn-shortDescription”] => string(20)“鲍勃   Massi发声“[”mvn-mitvAudioOnly“] => string(5)”false“   [ “MVN-fnc_mp3”] => array(0){} [“mvn-fnc_grab_50x50”] =>串(68)   “http://video.foxnews.com/thumbnails/102011/50/50/ff_massi_102011.jpg”   [ “MVN-fnc_grab_103x58”] =>串(69)   “http://video.foxnews.com/thumbnails/102011/103/58/ff_massi_102011.jpg”   [ “MVN-fnc_grab_156x88”] =>串(69)   “http://video.foxnews.com/thumbnails/102011/156/88/ff_massi_102011.jpg”   [ “MVN-flv1200”] =>串(60)   “http://media2.foxnews.com/102011/ff_massi_102011_FNC_MED.mp4”   [ “MVN-fnc_framework”] => array(0){} [“mvn-fnc_channel”] =>串(6)   “on_air”[“mvn-domain”] => array(0){} [“mvn-duration”] =>串(3)   “188”[“mvn-fnc_feedTag”] => array(0){} [“mvn-fnc_format”] =>   string(2)“NA”[“mvn-fnc_category”] =>串(35)   “on_air | fox_friends | shattered_dreams”[“mvn-fnc_category2”] =>   string(26)“personality | brian_kilmeade”[“mvn-fnc_show”] =>串(2)   “NA”[“mvn-fnc_default_playlist”] =>数组(0){}   [ “MVN-fnc_personality”] => string(2)“NA”[“mvn-fnc_mp4”] =>串(61)   “http://media2.foxnews.com/102011/ff_massi_102011_FNC_HIGH.mp4”   [ “MVN-fnc_grab_320x240”] =>串(70)   “http://video.foxnews.com/thumbnails/102011/320/240/ff_massi_102011.jpg”   [ “MVN-fnc_grab_90x70”] =>串(68)   “http://video.foxnews.com/thumbnails/102011/90/70/ff_massi_102011.jpg”   [ “MVN源”] => string(8)“Fox News”}}}}

当我这样做时:

$contents = file_get_contents("http://video.foxnews.com/v/feed/video/1228085488001.js"); 
var_dump(json_decode($contents->contents->channel->item->title, true));

输出:

NULL

如何从.js文件中获取这些变量:

contents->channel->item->**title**
contents->channel->item->media-content->**media-description**
contents->channel->item->media-content->**mvn-fnc_grab_103x58**

1 个答案:

答案 0 :(得分:3)

通过将true指定为json_decode的最后一个参数,您可以将其解析为关联数组。如果要使用对象表示法访问元素,请不要使用true作为最后一个参数:

$contents = file_get_contents("http://video.foxnews.com/v/feed/video/1228085488001.js"); 
var_dump(json_decode($contents));