在不同的 php 文件中解码 JSON stringify

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18957574/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:34:37  来源:igfitidea点击:

Decode JSON stringify in a different php file

phpjson

提问by Siri

<script>
function getstaff(){
var staffinfo = $('#group').val();
var myJSONText = JSON.stringify(staffinfo);
    $.ajax({
    type:"POST",
    data:{data:myJSONText},
    url:"staffDetails.php",
    success: function(result){
    alert(result);
    }
});
}
</script>

How do I retrieve the posted data in staffDetails.php file. It some how gives me drastic errors. Can anyone suggest how exactly to retrieve what I sent to this php file

如何检索staffDetails.php 文件中发布的数据。它是如何给我带来严重错误的。谁能建议如何准确地检索我发送到这个 php 文件的内容

<?php
$data = JSON.parse($_POST['data'],',');
echo $data;
?>

回答by trrrrrrm

Use json_decodeto decode a json array in PHP

使用json_decode在解码JSON数组PHP

 $data = json_decode($_POST['data']);

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-decode.php

回答by jeffery_the_wind

回答by developerCK

Try this:

尝试这个:

<?php

$data = json_decode($_POST['data'],true); 
print_f($data); // return data in associative array.

?>

回答by Code L?ver

In PHP there is function:

在 PHP 中有函数:

json_decode($_POST['data']);

use it.

用它。

JSON.parse 

is the function of Javascript so it will not work in php.

是 Javascript 的功能,所以它在 php 中不起作用。

Read this: json_decode.

阅读:json_decode

json_encode.

json_encode

回答by Bineesh

suppose you have array like

假设你有这样的数组

`{"1":{"1":"Employer+EID","2":"File+Creation+Date","3":"File+Creation+Time","4":"Payer+EID","5":"Payer+QID","6":"Payer+Bank+Short+Name","7":"Payer+IBAN","8":"Salary+Year+and+Montd","9":"Total+Salaries","10":"Total+records","11":"","12":"","13":"","14":"","15":""}}

you can read in php with :

你可以在 php 中阅读:

$data = json_decode($_POST['data'],true);

$data = json_decode($_POST['data'],true);

echo $data[1][1]; use logic to cycle through data.

回声 $data[1][1]; 使用逻辑来循环数据。