json_decode() returns null issues

前端 未结 10 2135
误落风尘
误落风尘 2020-12-03 11:47

I\'ve an issue with my JSON. It works returns correctly in PHP 5.3 (so I can\'t use json_last_error()), and it returns successfully when I copy string explicitly into json_d

相关标签:
10条回答
  • 2020-12-03 12:08

    Since PHP 7.3, the json_decode function will accept a new JSON_THROW_ON_ERROR option that will let json_decode throw an exception instead of returning null on error.


    Example:

    try {  
      json_decode("{", false, 512, JSON_THROW_ON_ERROR);  
    }  
    catch (\JsonException $exception) {  
      echo $exception->getMessage(); // displays "Syntax error"  
    }
    
    0 讨论(0)
  • 2020-12-03 12:13

    I notice this behavior with PHP version 5.14.12, and it may be for others versions as well.
    When using file_get_contents to load a JSON string into json_decode function, I had to strip out the BOM characters i.e. for UTF-8 EF BB BF before it would work properly.
    Compare the lengths of your two strings --the hard coded versus the passed in variable-- if they are not the same, these characters may be the culprit.

    0 讨论(0)
  • 2020-12-03 12:14

    Useful post from Sebastian about the json output format

    https://github.com/sebastianbergmann/phpunit/issues/142

    Using Keith's suggestion will allow the data to be parsed correctly

    0 讨论(0)
  • 2020-12-03 12:15

    Try this the problem is this html_entity_decode($your value);

     if(get_magic_quotes_gpc()){
       $param = stripslashes($row['your column name']);
     }else{
       $param = $row['your column name'];
    }
    
    
    $param = json_decode(html_entity_decode($param),true);
     $json_errors = array(
    JSON_ERROR_NONE => 'No error has occurred',
    JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
    JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
    JSON_ERROR_SYNTAX => 'Syntax error',
    );
     echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
    print_r($param);
    
    0 讨论(0)
  • 2020-12-03 12:20

    When I use:

    phpunit --log-json file.json <test_file>
    

    (using PHPUnit 3.4.13), The file that it creates does not appear to contain valid JSON,

    the json file contains "json" which looks something like:

    {...}{...}{...}{...}
    

    Instead of what I would expect to see:

    [{...},{...},{...},{...}]
    

    Not sure if the is the same problem that you're seeing, your sample JSON output in the question appears to be more valid that what I'm seeing.

    Once adding the missing commas and brackets, it can be parsed with json_decode() on PHP 5.2.10 or PHP 5.3.2.

    0 讨论(0)
  • 2020-12-03 12:24

    What a HORRENDOUS debug session.. well there's good news.. I figured it out..

    I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle &quot;, which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

    $json = str_replace('&quot;', '"', $json);
    

    Now I thought they were the same.. maybe someone can enlighten me..

    0 讨论(0)
提交回复
热议问题