Resolve PHP json decode error: Difference between revisions
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
* the input is empty entirely or combination of multiple white spaces | * the input is empty entirely or combination of multiple white spaces | ||
* the input contains [[Byte order mark]] (BOM)<ref>[https://blog.longwin.com.tw/2013/11/php-json_decode-null-fix-2013/ PHP json_decode() 傳回 NULL 的解法 | Tsung's Blog]</ref> | * the input contains [[Byte order mark]] (BOM)<ref>[https://blog.longwin.com.tw/2013/11/php-json_decode-null-fix-2013/ PHP json_decode() 傳回 NULL 的解法 | Tsung's Blog]</ref> | ||
* One file was combined with multiple JSON | * One file was combined with multiple JSON which were separated by return symbol | ||
* Use {{kbd | key=<nowiki>'</nowiki>}} instead of {{kbd | key=<nowiki>"</nowiki>}} for quotation | * Use {{kbd | key=<nowiki>'</nowiki>}} instead of {{kbd | key=<nowiki>"</nowiki>}} for quotation | ||
Revision as of 16:05, 8 May 2019
Syntax error, malformed JSON
Possible causes
- the input is empty entirely or combination of multiple white spaces
- the input contains Byte order mark (BOM)[1]
- One file was combined with multiple JSON which were separated by return symbol
- Use ' instead of " for quotation
Solution:
- Not allowed empty input
- Remove Byte order mark (BOM)
- Use " for quotation
- Use JSONLint - The JSON Validator to validate the JSON format
Unexpected control character found
Possible causes
- the input string contains control characters
Solution:
- Remove control character. Using PHP:
$replacement = ''; preg_replace('/[\x00-\x1F]/', $replacement, $input);