开发者

json_decode returns null

开发者 https://www.devze.com 2023-01-27 13:20 出处:网络
I store encoded 开发者_如何学Pythonarrays in a database, and when I try to decode it, it will return with null.

I store encoded 开发者_如何学Pythonarrays in a database, and when I try to decode it, it will return with null.

[{"id":13,"qty":"1"}]

Arrays are encoded with PHP, so I have no idea what the problem might be.

Thanks


Personally the serialize(); and unserialize(); functions would be better if you want to store an array in a database. JSON functions should be used for JSON strings to be returned to JavaScript.

$array = array('data','more data','superdata'=>'even more data');
$array = serialize($array);
// Store array in database after serialized

Then to turn the data back into an array from the database, you would...

// Fetch the data from the database
$data = $row['myDataArray'];
$array = unserialize($data);
echo $array['superdata']; // Would produce: "even more data"

Hopefully this will help you out a bit with your issues. If you did need to turn it into JSON to send to JavaScript, then you would use the json_encode($array); function.


Try to print:

json_last_error()


I had a similar issue, though in a different context. In my case, I was trying to decode a JSON string, encoded with Javascript JSON.stringify, store it in a hidden text field and decode it back in PHP after the page is POSTed. I found, applying a stripslashes function before decoding was helpful.

$arrMyOutput = json_decode(stripslashes($yourJSON_string_goes_here));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号