我正在尝试使用php中的json_decode解析json。 它是一个url失败,任何人都可以告诉我它为什么失败并且在php中有json_decode的替代品吗?
这是我的代码
$url='https://espn.go.com/travel/sports/calendar/getList.json?&xhr=1&date=20121027&type=list&query=null&myTeams=';
$html = file_get_html($url);
$json=json_decode($html,true);
// json在这里为空
答案 0 :(得分:5)
file_get_html
是PHP Simple HTML DOM Parser Not default PHP function
应该是
$html = file_get_contents($url);
请注意,返回的JSON有错误的UTF-8字符,可能编码错误
修复此问题
$url = 'http://espn.go.com/travel/sports/calendar/getList.json?&xhr=1&date=20121027&type=list&query=null&myTeams=';
$html = file_get_contents($url);
$json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));
$json = json_decode($json);
echo "<pre>";
print_r($json);
输出
tdClass Object
(
[nfb] => Array
(
[0] => stdClass Object
(
[events] => Array
(
[0] => stdClass Object
(
[id] => 265911
[time] => 12:00 AM ET
[sportId] => 23
[link] => http://espn.go.com/ncf/team/_/name/
[prevLink] => http://espn.go.com/ncf/preview?gameId=323010002
[recapLink] => http://espn.go.com/ncf/recap?gameId=323010002
[shortSport] => ncaa
[homeId] => 2
[awayId] => 245
[homeScore] => -1
... So Many More