php 从 $_POST 中的 json 读取关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5806971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read associative array from json in $_POST
提问by Daniel
I am using jQuery to post a json object to my php application.
我正在使用 jQuery 将 json 对象发布到我的 php 应用程序。
jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });
The json string as pulled from firebug looks like this
从 firebug 中提取的 json 字符串如下所示
{ "data" : [ { "contents" : "This is some content",
"selector" : "DIV.subhead"
},
{ "contents" : "some other content",
"selector" : "LI:nth-child(1) A"
}
],
"page" : "about_us.php"
}
In php I am trying to turn this into an associative array.
在 php 我试图把它变成一个关联数组。
My php code so far is
到目前为止,我的 php 代码是
<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>
The response to the ajax call should be "about_us.php" but it comes back blank.
对 ajax 调用的响应应该是“about_us.php”,但它返回空白。
采纳答案by Francesco Terenzani
You can avoid to use JSON.stringify
and json_decode
:
您可以避免使用JSON.stringify
和json_decode
:
jQuery.post("save.php", dataToSend, function(data){ alert(data); });
And:
和:
<?php
echo $_POST['page'];
?>
Update:
更新:
... but if your really want to use them, then:
...但如果你真的想使用它们,那么:
jQuery.post("save.php", {json: JSON.stringify(dataToSend)}, function(data){ alert(data); });
And:
和:
<?php
$value = json_decode($_POST['json']);
echo $value->page;
?>
回答by Evert
$_POST
will not be populated if the request body is not in the standard urlencoded form.
$_POST
如果请求正文不是标准的 urlencoded 形式,则不会填充。
Instead, read from the read-only php://input
streamlike this to get the raw request body:
相反,像这样从只读php://input
流中读取以获取原始请求正文:
$value = json_decode(file_get_contents('php://input'));
回答by Shakti Singh
Pass the second argument as true if you want the associative array otherwise it will keep returning object.
如果您想要关联数组,则将第二个参数传递为 true,否则它将继续返回对象。
$value = json_decode(stripslashes($_POST),true);
回答by Paul DelRe
It looks like jQuery might encode a javascript object in urlencoded form then would be populated into $_POST. At least from their examples. I'd try passing in your object into post()
without stringifying it.
看起来 jQuery 可能会以 urlencoded 形式对 javascript 对象进行编码,然后将填充到 $_POST 中。至少从他们的例子来看。我会尝试将您的对象传入post()
而不对其进行字符串化。
回答by karim79
Try:
尝试:
echo $value->page;
since json_decode
's default behaviour is to return an object of type stdClass
.
因为json_decode
的默认行为是返回一个类型的对象stdClass
。
Alternatively, set the second optional $assoc
argument to true
:
或者,将第二个可选$assoc
参数设置为true
:
$value = json_decode(stripslashes($_POST), true);
echo $value['page'];
回答by Mukesh
If you want to use json data as an associative array, you can try as following:
如果你想使用 json 数据作为关联数组,你可以尝试如下:
<?php
$json = 'json_data'; // json data
$obj = jsondecode($json, true); // decode json as associative array
// now you can use different values as
echo $obj['json_string']; // will print page value as 'about_us.php'
for example:
$json = { "data" : [ { "contents" : "This is some content",
"selector" : "DIV.subhead"
},
{ "contents" : "some other content",
"selector" : "LI:nth-child(1) A"
}
],
"page" : "about_us.php"
}
$obj = json_decode($json, true);
/* now to print contents from data */
echo $obj['data']['contents'];
// thats all
?>