如何在Laravel 5中创建嵌套的json响应?例如:
"response":["status":"OK",
"data":{ "user":{"name": "Shriyansh",
"email":"some@email.com",
"contact":"1234567890",
"fcmToken":"Token@123"
},
"event":{"status":"successful",
"status_code":4
}
}
]
答案 0 :(得分:1)
创建一个数组以保存您的响应:
$data = [
"status"=> "OK",
"data"=> [
"user" => [
"name"=> "Shriyansh",
"email"=>"some@email.com",
"contact"=>"1234567890",
"fcmToken"=>"Token@123"
],
"event"=> [
"status" => "successful",
"status_code" => 4
]
]
]
然后使用Laravel响应方法返回您的数据,如下所示:
return response()->json($data, 200);
因此,Laravel会将您的数组转换为json格式并将其发送回您的客户端。
答案 1 :(得分:1)
你应该试试这个:
$json = json_encode(array(
"status"=>"OK",
"user" => array(
"name"=> "Shriyansh",
"email"=>"some@email.com",
"contact"=>"1234567890",
"fcmToken"=>"Token@123"
),
"event" => array(
"status" => "successful",
"status_code" => 4
)
));
答案 2 :(得分:0)
您可以使用JSON Response在Laravel中返回JSON。
例:
假设您在$data
变量中有所需的数据,
return response()->json([
'data' => $data
],200);
其中,200是状态代码。状态代码将按照laravel的状态代码添加。