Php 如何从json中的json返回数据
我试图在JSON中返回JSON。我试过了,但我想不出来 Controller.phpPhp 如何从json中的json返回数据,php,json,laravel,Php,Json,Laravel,我试图在JSON中返回JSON。我试过了,但我想不出来 Controller.php $output_sum = json([ "sold_price"=>1, "item_cost"=> 2, "fees"=>3, "profit"=>4 ]); return response()->json([ 'spreadsheet_grid'=>"hello", 'spreadsheet_summary'=>$output_sum ]
$output_sum = json([
"sold_price"=>1,
"item_cost"=> 2,
"fees"=>3,
"profit"=>4
]);
return response()->json([
'spreadsheet_grid'=>"hello",
'spreadsheet_summary'=>$output_sum
]);
我认为将整个阵列编码一次会更好、更简单,如:
$output_sum = [
"sold_price"=>1,
"item_cost"=> 2,
"fees"=>3,
"profit"=>4
];
return response()->json([
'spreadsheet_grid' => "hello",
'spreadsheet_summary' => $output_sum
]);
生成的JSON格式如下所示:
{
"spreadsheet_grid":"hello",
"spreadsheet_summary":
{
"sold_price":1,
"item_cost":2,
"fees":3,
"profit":4
}
}
你要对它进行两次编码,所以你必须对它进行两次解码。你想将json作为字符串或对象返回吗?作为对象@JerodevI想知道为什么你要这样做,而不是创建一个对象并对其进行编码。无需将
output\u sum
转换为json
(我想)这是一个@Sang示例