简体   繁体   English

用PHP接收和发送回JSON数据

[英]Receiving and sending back json data in PHP

I am sending data from services.js coming in as a json encoded string in the following format to myPhp.php: 我正在以以下格式将来自service.js的数据作为json编码的字符串发送到myPhp.php:

{"Name":"alpha"}

I want to gather this data and send back to the services.js as it came back in json form adding beta to the string as in: 我想收集这些数据并以json形式返回给services.js,将beta添加到字符串中,如下所示:

{"Name":"alpha beta"}

myPhp.php myPhp.php

<?php
$a = $_POST[data];
$b = json_decode($a);
$c = $b.Name + "beta";
echo ($c);
?>
<?php
    $a = $_POST[data];
    $b = json_decode($a);
    $c = $b['Name'] . " beta";
    header('Content-Type: application/json');
    echo json_encode($c);
?>

The "." “。” notation is not used in PHP to access / set properties - more like Javascript and you need to have quotes around the $_POST variable ~ unless data is defined as a constant. PHP中不使用符号来访问/设置属性-更像Javascript,除非data定义为常量,否则您需要在$_POST变量附近加上引号〜。 I think you could try something like this though. 我认为您可以尝试这样的操作。

<?php
    $b = json_decode( $_POST['data'],true );
    $b['name'].='beta';
    echo json_encode($b);
?>

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

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