为什么在 JSON.stringify 之后的 JSON 中为 NULL


why NULL in a json after JSON.stringify?

php 5.4 fastcgijquery 1.10

Jquery代码:

$.ajax({
          type: "POST",
          url: "",
          dataType: "json",
          data: { json: JSON.stringify({test: 'teste'}) }
        }).done(function(msg) {
              var msg = $.parseJSON(msg);
              alert(msg);
        });

PHP代码:

$json = $_POST['json'];
$info = json_decode($json, true);
var_dump($info);

结果:

array(1) {
  ["test"]=>
  string(5) "teste"
}
null

我不知道为什么这个应用程序以及如何删除它。因为如果我尝试使用:

$i = info['test'];
echo $i;

我将收到:睾丸

似乎您的 JSON 数据是问题所在。

PHP

中的json_decode()将 JSON 编码的字符串作为输入并将其转换为 PHP 变量。

它的工作原理是这样的

<?php
$json = '{"test": 12345}';
$obj = json_decode($json);
print $obj->{'test'}; // 12345
?>