php使用json_decode返回NULL

php5.2之後自帶json_decode函數,可是對json文本串的格式要求很是嚴格。 php

極可能使用該函數獲得的返回值是NULL json

能夠使用使用json_last_error()函數獲取到的返回值來幫助咱們判斷出問題的緣由。 函數

其中若是提示錯誤JSON_ERROR_SYNTAX(Syntax error),表示json串格式錯誤。 編碼

能夠經過如下幾個方式排錯: code

1. json字符串必須以雙引號包含 字符串

$output = str_replace("'", '"', $output); it

2. json字符串必須是utf8編碼
$output = iconv('gbk', 'utf8', $output); ast

3.不能有多餘的逗號 如:[1,2,]
用正則替換掉,preg_replace('/,\s*([\]}])/m', '$1', $output) error

相關文章
相關標籤/搜索