当json响应里面的json响应时,php中的json响应无效

时间:2018-03-01 11:32:16

标签: json drupal jsonresponse

我已尝试打印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_encode

中删除echo json_encode($php);

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

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

您也可以这样做,

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