Php7 json_decode null

1. Confirm that the file has no BOM header

2. Tried the following ways to remove illegal strings, but still output NULL

        $some_string = htmlspecialchars_decode($some_string);

        $some_string = preg_replace("/\t/", " ", $some_string);
        $some_string = preg_replace("/\n/", " ", $some_string);
        $some_string = str_replace("\n", " ", $some_string);
        $some_string = str_replace ("\n","", $some_string);

3, json_last_error () output 4pm Syntax error, malformed JSON

4. Output the string directly, and the browser can parse josn normally, as shown in the screenshot

Php
Mar.21,2022

because your string is not a standard JSON string, the standard JSON string causes

by using "" for every string type.

Test Code

<?php 
$jsonStr1 = '{status: {RetCode:0, msg: "success"}, data: {}}';
var_dump(json_decode($jsonStr1, true));
var_dump(json_last_error());

echo "----------------".PHP_EOL;

$jsonStr2 = '{"status": {"RetCode":0, "msg": "success"}, "data": {}}';
var_dump(json_decode($jsonStr2, true));

result

NULL
int(4)
----------------
array(2) {
  ["status"]=>
  array(2) {
    ["RetCode"]=>
    int(0)
    ["msg"]=>
    string(7) "success"
  }
  ["data"]=>
  array(0) {
  }
}

= Update =

after debugging, it is found that it is caused by BOM . Here is the solution

$dataString = $merchant_arr['data'];
$A = substr($dataString, 0, 1);
$B = substr($dataString, 1, 1);
$C = substr($dataString, 2, 1);
if ((ord($A) == 239) && (ord($B) == 187) && (ord($C) == 191)) {
    $dataString = substr($dataString, 3);
}
$dataArray = json_decode($dataString, true);

json_decode($jsonStr, true);

have you added the parameter true?


information is relatively small, it is difficult to judge, post $some_string to have a look, or explain where this value is obtained.


$str="json" ;                       
dump($json_decode($str));               
echo $errorinfo=json_last_error();            
4  
htmlspecialchars_decode$str;         
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e489c6-44cbc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e489c6-44cbc.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?