PHP solves the problem of json_decode parsing json string failure

The string required by json_decode is strict:

  1. Use UTF-8 encoding
  2. Cannot have a comma in the last element
  3. Single quotes cannot be used
  4. There cannot be \r,\t, please replace if there are

Encounter problems:

Some editors will add the BOM header by default when returning the json string, which will cause the parsing to fail.

solution:

//解决返回的json字符串中返回了BOM头的不可见字符(某些编辑器默认会加上BOM头)
$result = trim($result,chr(239).chr(187).chr(191));
//解析json成数组
$array=json_decode($result,true);

 

Guess you like

Origin blog.csdn.net/qq15577969/article/details/113794402