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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.