在谷歌搜索、逐个代码实现之后,我仍然无法显示任何内容。没有。不是一件事。
我有一个输出 JSON 的 URL:
{"videos":[{"video":{"name":"Sanyo Zio","youtube":"FxxLDr--R5A","post_date":"2010-10-08 01:00:00",...
这是我用来访问该页面的代码:
$url = file_get_contents("http://[website]/json/test.json");
$arr = json_decode($url,true);
现在这是我尝试从页面访问任何数据的简短列表:
1:
print_r($arr);
2:
foreach($arr['videos']['video'] as $item) {
echo "Name: ". $item[0] ."<br>";
}
3:
$obj = $arr[0];
echo $obj;
4:
foreach($arr as $a){
echo "Name: ".$a['videos']['video']['name']."<br />";
}
显然我遗漏了一些东西,但我一直无法弄清楚我做错了什么!我的编码不正确吗?以下是我对 JSON 进行编码的方式:
$arr = array('videos' => array());
foreach($vid as $items){
$arr['videos'][] = array('video' => array(
'name' => $items['videoName'], 'youtube' => $items['youtubeID'], 'post_date' => $items['productionTimestamp'], 'description' => $items['videoDesc'], 'link' => $single_linker_values['deeplink'], 'image' => $image));
}
echo json_encode($arr);
有什么想法/建议吗?
更新 - 显然服务器已锁定,但在内部我没有注意到 :) 显然网页确实如此!感谢您的帮助!
最佳答案
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit
由于您没有指定递归限制,很可能是您的 JSON 无效或者没有从您的 URL 检索到任何内容。
尝试三件事:
判断是否存在 json_decode 错误
print_r(json_last_error());//在 json_decode 之后调用
检查是否返回数据
print_r($url);
查看数据是否将解码为对象
$obj = json_decode($url);
print_r($obj);
关于php - 使用 PHP 从 URL 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14199363/