我正在尝试使用 JSON 解码来检索一些信息,但它不起作用,它只是在我使用 var_dump 时将数据显示为空
这是在 URL 中传递的 JSON 格式的数据
orderSummary={"orderInfo":[{"itemNumber":"1","quantity":"3","price":"5.99","productName":"Item_B"}]}
当我简单地回显未解码的字符串时,我得到以下内容
echo $_GET['orderSummary'];
//displays the following
{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}
但是,当我尝试对其进行解码时,结果为空
$order = $_GET['orderSummary'];
$data = json_decode($order,true);
echo "<PRE>";
var_dump($data); die();
//displays the following
<PRE>NULL
是不是格式不对?
最佳答案
通过stripslashes()
运行输入字符串首先。
$input = '{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}';
print_r(json_decode(stripslashes($input)));
输出
stdClass Object
(
[orderInfo] => Array
(
[0] => stdClass Object
(
[itemNumber] => 1
[quantity] => 3
[price] => 5.99
[productName] => Item_B
)
)
)
或者
关闭 magic_quotes_gpc
.考虑到它已被弃用(并在 5.4 中删除),这是更好的选择。
关于PHP json_decode 不工作 - 显示 NULL 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555678/