Jump to content

json_decode error for empty string


NotionCommotion

Recommended Posts

The following script produces the following results when run on the two machines:

<?php
$json='';
$obj=json_decode($json);
$error=json_last_error();
$version=phpversion();
echo("$version | $error");
5.6.30 | 0
7.1.6 | 4

http://php.net/manual/en/function.json-decode.php tells me:

7.1.0 An empty JSON key ("") can be encoded to the empty object property instead of using a key with value _empty_.
7.0.0 An empty PHP string or value that after casting to string is an empty string (NULL, FALSE) results in JSON syntax error.

 

 

How do you interpreter these two lines?

   

 

Link to comment
Share on other sites

Right. It's a change in behavior: before they were using "_empty_" so that people could access it easily,

$object->_empty_
but with the complex {} syntax becoming more and more well known (especially when it comes to decoding a JSON object) PHP stopped "fixing" the key so that

$object->{""}
would work as expected.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.