Home  >  Article  >  Backend Development  >  PHP uses json_decode to return NULL_PHP tutorial

PHP uses json_decode to return NULL_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:07:191103browse

PHP5.2 and later comes with the json_decode function, but the format requirements for json text strings are very strict.

It is very likely that the return value obtained by using this function is NULL
The return value obtained by using the json_last_error() function is JSON_ERROR_SYNTAX (Syntax error).
You can troubleshoot in the following ways.
1. The json string must be enclosed in double quotes
$output = str_replace("'", '"', $output);
2. The json string must be utf8 encoded
$output = iconv('gbk', 'utf8', $output);
3. There can be no extra commas, such as: [1,2,]
Replace with regular expression, preg_replace('/,s*([]}])/m', '$1', $output)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477869.htmlTechArticlephp5.2 comes with the json_decode function, but the format requirements for the json text string are very strict. It is very likely that the return value obtained by using this function is NULL. The return value obtained by using the json_last_error() function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn