为什么json_decode($ data,TRUE)将数组转换为字符串? [英] Why is json_decode($data, TRUE) converting an array to a string?
问题描述
JavaScript代码:
$。ajax({
type:POST,
url :postTestingResult.php,
data:{data:JSON.stringify(sendData)},
dataType:json,
成功:ajaxSuccess,
错误:ajaxError
});
PHP代码
$ data = json_decode($ _ POST ['data'],TRUE);
当我将复杂的数据结构发布到服务器时,最外层的数组正在变成一个字符串。例如,JavaScript对象可能是
var data = {apps:[[1,2,3],[ 4,5,6]]}
使用JSON.stringify(数据)这就变成了
{apps:[[1,2,3],[4,5,6]]}//作为通过Chrome控制台中的console.log(数据)看到
但是在执行了json_decode($ _ POST [' data'],TRUE)它变成
array('apps'=>'[[1,2,3], [4,5,6]]')//通过var_export($ data,TRUE)看到
这里发生了什么?为什么数组被转换为字符串?要查看完整的JSON对象和完整的PHP对象,请:
$ data = array();
$ json = file_get_contents('php:// input'); //从原始POST数据中读取JSON
if(!empty($ json)){
$ data = json_decode($ json,true); // decode
}
print_r($ data);
收益率:
数组(
[apps] =>数组(
[0] =>数组(
[0] => 1
[1] = > 2
[2] => 3)
[1] =>数组(
[0] => 4
[1] => 5
[2] => 6
)
))
希望这会有所帮助:)
编辑
请注意 PHP文档声明:
注意:使用php://输入打开的流只能读取一次;流不支持搜索操作。
但是,iirc已经或将要改变(可能在PHP 5.6中?)。不要引用我的话,但是现在,如果你计划重复使用它,不要忘记分配该流的内容!
JavaScript Code:
$.ajax({
type: "POST",
url: "postTestingResult.php",
data: {data: JSON.stringify(sendData)},
dataType: "json",
success: ajaxSuccess,
error: ajaxError
});
PHP Code
$data = json_decode($_POST['data'], TRUE);
When I POST a complex data structure to the server, the outermost array is becoming a string. For example, the JavaScript object could be
var data = {"apps": [[1,2,3], [4,5,6]]}
Using JSON.stringify(data) this becomes
"{"apps": "[[1,2,3], [4,5,6]]"}" //As seen via console.log(data) in Chrome console
But after doing the json_decode($_POST['data'], TRUE) it becomes
array('apps' => '[[1,2,3], [4,5,6]]') //As seen via var_export($data, TRUE)
What's going on here? Why is the the array being converted to a string? To see the full JSON object and the full PHP object check out this pastebin with the two.
Any help is greatly appreciated, thank you.
UPDATE: Answer found I found the main culprit. I am also using Prototype.js and it was adding a toJSON method to the Object prototypes. Check out this SO question for details.
Try this. Send your data explicitly as application/json
and don't wrap your sendData
:
var sendData = {'apps': [[1,2,3], [4,5,6]]};
$.ajax({
type: 'POST',
url: 'postTestingResult.php',
data: JSON.stringify(sendData), // don't wrap your JSONified object
contentType: 'application/json' // set application/json - default is x-form-urlencoded
});
Note the headers and data: application/json
:
Of course, as you highlighted, the data will not be available in the $_POST
superglobal now. However this is not an issue, a very common way to get the JSON data string is to read the raw post data via php://input
:
$data = array();
$json = file_get_contents('php://input'); // read JSON from raw POST data
if (!empty($json)) {
$data = json_decode($json, true); // decode
}
print_r($data);
Yields:
Array(
[apps] => Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3 )
[1] => Array (
[0] => 4
[1] => 5
[2] => 6
)
))
Hope this helps :)
EDIT
Note that the PHP documentation states:
Note: A stream opened with php://input can only be read once; the stream does not support seek operations.
However, iirc this has or will change (possibly in PHP 5.6?). Don't quote me on that though, and for now, don't forget to assign the contents of that stream if you plan to reuse it!
这篇关于为什么json_decode($ data,TRUE)将数组转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!