简体   繁体   English

json_decode返回json数据

[英]json_decode return json data

Im just posting single data value like this '{'email':'whatever@yahoo.com'}' 我只是发布像这样的单个数据值'{'email':'whatever@yahoo.com'}'

php file php文件

$var = json_decode($_POST,true);
echo json_encode($var["email"]);

at this stage i just want to return the email address to get it working buts its giving me this error: 在这个阶段,我只想返回电子邮件地址以使其正常工作,但是它给了我这个错误:

json_decode() expects parameter 1 to be string, array given in C:\\wamp\\www\\buyme\\include\\getemailaddress.php on line 4 json_decode()期望参数1为字符串,第4行的C:\\ wamp \\ www \\ buyme \\ include \\ getemailaddress.php中给出的数组

line 4 is the first line in my code 第4行是我代码中的第一行

all i want to be able todo is access the email value and return it back in json_encode($var["email"]) 我想要做的就是访问电子邮件值并以json_encode($ var [“ email”])的形式返回。

If I got you correctly and your posting a json string you can do: 如果我正确理解了您并发布了json字符串,则可以执行以下操作:

$requestBody = @file_get_contents('php://input');
$var = json_decode($requestBody, true);
echo json_encode($var['email']);

As you can read in PHP Manual $_POST , $_POST contains values in associative array. 如您在PHP手册$ _POST中所读,$ _POST包含关联数组中的值。 As in manaul too, it is : 就像在manaul中一样,它是:

An associative array of variables passed to the current script via the HTTP POST method.

So if you are sending any json string from client-end in any variable, Use that variable to read that json string like this. 因此,如果您要通过任何变量从客户端发送任何json字符串,请使用该变量读取该json字符串,如下所示。

$var = json_decode($_POST['emaildata'],true);
echo json_encode($var["email"]);

Please check, if it works for you.. 请检查,是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM