[英]Why is PHP json_decode on JSON loaded from a database not working?
我正在使用json_encode(["something" => ["hello", "world"]])
对 JSON 进行编码,然后将其保存到数据库中。
数据库存储 JSON,如下所示:
{"something":["hello","world"]}
当使用json_decode($data)
从数据库中解码这些数据,然后 print_ring 打印结果如下:
{"something":["hello","world"]}
在尝试解码之前,我已经对数据库中的数据进行了以下尝试:
$data = urldecode($data);
$data = htmlspecialchars_decode($data);
$data = str_replace(""", "\"", $data);
$data = str_replace(""", "\\\"", $data);
$data = str_replace("\u0022","\\\\\"", $data);
但没有成功。 我究竟做错了什么? 我该如何解决?
所以,如果我正确理解了这个问题,你可以对数据进行编码(变成一个字符串)并存储它。 然后您检索字符串并对其进行解码。 它又变成了一个字符串?
最有可能的是,您应该在插入和检索时查看字符串 escaping。 见https://www.php.net/manual/en/pdo.quote.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.