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 格式
多謝版主!!