php json_decode輸出null, json_last_error() 頁面顯示4,也就是語法錯誤。
個人覺得出現這樣的問題只要是json字符串裏存在反斜槓/,或者有中文.
解決方法一:
出現這個問題是因爲在 json 字符串中反斜槓被轉義,只需要用 htmlspecialchars_decode() 函數處理一下 $content 即可:
$content = htmlspecialchars_decode($content);
此時再使用 json_decode() 函數解析,就沒有問題了;
解決方法二:
在保存 json 數據時使用 urlencode() 函數:
$content = urlencode(json_encode($content));
解析時使用 urldecode() 函數:$content = urldecode($content);
即可避免反斜槓轉義造成的無法解析。
參考文章:點擊打開鏈接