我有以下 JSON 字符串,我尝试用 php json_decode 解码,但是 $postarray 始终为 NULL,无法弄清楚这是为什么?
在 Debian 5.0 Linux 上运行 php 客户端 API 版本 => 5.0.51a JSON版本1.2.1
$json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';
$postarray = json_decode($json);
print_r($postarray);
谢谢
最佳答案
在字符串中转义双引号 (\"
) 的原因是,如果字符串是双引号。
因为你要转义双引号,你应该用双引号(而不是单引号)引用你的字符串,像这样:
<?php
$json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";
$postarray = json_decode($json);
print_r($postarray);
?>
如果您确实想对字符串进行单引号,则使用 don't escape the double quotes ,或者像 Andrei 建议的那样使用 stripslashes()。
您可以阅读有关 the four ways to specify a string in PHP, and the differences among them, here 的信息。
关于php - 无法在php中解码JSON字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3586401/