当前位置:Gxlcms > PHP教程 > 怎么死都打不开json的数据啊_(:з」∠)_

怎么死都打不开json的数据啊_(:з」∠)_

时间:2021-07-01 10:21:17 帮助过:28人阅读

http://202.103.244.125/catst.php
如上,刚接触PHP被这弄得好焦灼!json_decode出来的永远都是null啊!!!网上的方法也都试了,求大神帮忙


回复讨论(解决方案)

我就不相信你都试了

$url = 'http://202.103.244.125/catst.php';$s = file_get_contents($url);print_r(json_decode(substr($s, 3)));
Array(    [0] => stdClass Object        (            [menutype] => A            [groupx] => 0            [titleng] => 凉菜             [titlech] => Cold Dish        )    [1] => stdClass Object        (            [menutype] => A            [groupx] => 1            [titleng] => 汤 粥            [titlech] => Soup Porrdg        )    [2] => stdClass Object        (            [menutype] => A            [groupx] => 2            [titleng] => 烫菜            [titlech] => Hot food        )    [3] => stdClass Object        (            [menutype] => A            [groupx] => 3            [titleng] => 素菜            [titlech] => Veget -able        )    [4] => stdClass Object        (            [menutype] => A            [groupx] => 4            [titleng] => 主食            [titlech] => Main Food        )    [5] => stdClass Object        (            [menutype] => A            [groupx] => 5            [titleng] => 烤鱼 啤酒鱼            [titlech] => 烤鱼 啤酒鱼        )    [6] => stdClass Object        (            [menutype] => A            [groupx] => 6            [titleng] => 干锅 汤锅            [titlech] => Gangou Hotpot        )    [7] => stdClass Object        (            [menutype] => A            [groupx] => 7            [titleng] => 铁板 煲仔            [titlech] => Sheet Hotpot        )    [8] => stdClass Object        (            [menutype] => A            [groupx] => 8            [titleng] => 砂锅            [titlech] => Sha Guo        )    [9] => stdClass Object        (            [menutype] => A            [groupx] => 9            [titleng] =>             [titlech] =>         )    [10] => stdClass Object        (            [menutype] => A            [groupx] => 10            [titleng] => 特别 推荐            [titlech] => Special Food        )    [11] => stdClass Object        (            [menutype] => A            [groupx] => 11            [titleng] => 十八酿            [titlech] => 十八酿        )    [12] => stdClass Object        (            [menutype] => A            [groupx] => 12            [titleng] => 饮料 茶            [titlech] => Drink Tea        )    [13] => stdClass Object        (            [menutype] => A            [groupx] => 13            [titleng] => 其他 自定义            [titlech] => 其他 自定义        )    [14] => stdClass Object        (            [menutype] => A            [groupx] => 14            [titleng] => 白酒 啤酒            [titlech] => Wine Beer        ))

弄个 BOM 头害自己

http://202.103.244.125/catst.php
如上,刚接触PHP被这弄得好焦灼!json_decode出来的永远都是null啊!!!网上的方法也都试了,求大神帮忙


正解

我就不相信你都试了

$url = 'http://202.103.244.125/catst.php';$s = file_get_contents($url);print_r(json_decode(substr($s, 3)));
Array(    [0] => stdClass Object        (            [menutype] => A            [groupx] => 0            [titleng] => 凉菜             [titlech] => Cold Dish        )    [1] => stdClass Object        (            [menutype] => A            [groupx] => 1            [titleng] => 汤 粥            [titlech] => Soup Porrdg        )    [2] => stdClass Object        (            [menutype] => A            [groupx] => 2            [titleng] => 烫菜            [titlech] => Hot food        )    [3] => stdClass Object        (            [menutype] => A            [groupx] => 3            [titleng] => 素菜            [titlech] => Veget -able        )    [4] => stdClass Object        (            [menutype] => A            [groupx] => 4            [titleng] => 主食            [titlech] => Main Food        )    [5] => stdClass Object        (            [menutype] => A            [groupx] => 5            [titleng] => 烤鱼 啤酒鱼            [titlech] => 烤鱼 啤酒鱼        )    [6] => stdClass Object        (            [menutype] => A            [groupx] => 6            [titleng] => 干锅 汤锅            [titlech] => Gangou Hotpot        )    [7] => stdClass Object        (            [menutype] => A            [groupx] => 7            [titleng] => 铁板 煲仔            [titlech] => Sheet Hotpot        )    [8] => stdClass Object        (            [menutype] => A            [groupx] => 8            [titleng] => 砂锅            [titlech] => Sha Guo        )    [9] => stdClass Object        (            [menutype] => A            [groupx] => 9            [titleng] =>             [titlech] =>         )    [10] => stdClass Object        (            [menutype] => A            [groupx] => 10            [titleng] => 特别 推荐            [titlech] => Special Food        )    [11] => stdClass Object        (            [menutype] => A            [groupx] => 11            [titleng] => 十八酿            [titlech] => 十八酿        )    [12] => stdClass Object        (            [menutype] => A            [groupx] => 12            [titleng] => 饮料 茶            [titlech] => Drink Tea        )    [13] => stdClass Object        (            [menutype] => A            [groupx] => 13            [titleng] => 其他 自定义            [titlech] => 其他 自定义        )    [14] => stdClass Object        (            [menutype] => A            [groupx] => 14            [titleng] => 白酒 啤酒            [titlech] => Wine Beer        ))

好像真的行了!!!
请问那个substr()的参数为什么是3啊?如果处理其他的json文件也是3吗?还是需要做个判定呢?

utf-8 的BOM 头,是三个字符,所以用 substr 跳过前 3 个字符
既然是要输出 json 那么程序文件要保存为 utf-8 无 BOM 格式

多谢版主!!

人气教程排行