且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

json_decode在php中返回null

更新时间:2023-12-04 14:19:40

file_get_html PHP简单HTML DOM解析器 Not default PHP function应该是

 $html = file_get_contents($url);     

请注意,返回的JSON格式错误,可能是UTF-8字符格式错误,可能编码错误

Please also Note that the returned JSON has an error of Malformed UTF-8 characters, possibly incorrectly encoded

解决此问题

$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

查看实时演示