-1

我尝试打印json response""被添加到我的json response

我尝试了下面的代码php并由iddrupal field

<?php
$data = array("title"=>"test","body"=>"test body");
$php = json_encode($data,JSON_FORCE_OBJECT);
echo json_encode(array("php"=>$php,"id"=>10));
?>

输出 :

{"php":"{\"title\":\"test\",\"body\":\"test body\"}","id":10}

但我想要如下输出

{"php":{"title":"test","body":"test body"},"id":10}

我为上述问题添加了更多代码

{"php":"{\"title\":\"test\",\"body\":\"test body\"}","id":10}

为什么不json_encodeecho json_encode($php);

我如何才能第二次获得高于输出

4

2 回答 2

3

您在 JSON 中编码两次:

$data = array("title"=>"test","body"=>"test body");
$php = json_encode($data,JSON_FORCE_OBJECT);
echo $php ; // remove json_encode() here

问题编辑后:

$data = array("title"=>"test","body"=>"test body");
echo json_encode(array("php"=>$data,"id"=>10));
于 2018-03-01T11:34:10.333 回答
0

你也可以简单地这样做,

<?php
    $data = array("title"=>"test","body"=>"test body");
    echo json_encode(array("php"=>$data,"id"=>10));
?>
于 2018-03-01T11:36:14.847 回答