将JSON嵌入“数据”

问题描述 投票:-2回答:1

我目前以这种格式将JSON返回给ajax调用: [ { “用户名”: “TEST3”, “点”:62, “sportName”: “篮球”}, { “用户名”: “TEST2”, “点”:55, “sportName”: “足球”} ]

但我希望这样: { “数据”: [ {“username”:“test3”,“points”:“62”,“sportName”:“Basketball”}, {“username”:“test2”,“points”:“55”,“sportName”:“足球”} ] }

返回它的函数(Laravel):

$scores=DB::select("SELECT user.username, points
                    FROM scores
                    JOIN users AS user ON user.id = scores.user_id
                    ORDER BY points DESC");

return json_encode($scores);
php json laravel
1个回答
0
投票

你可以这么做。

return json_encode( ['data' => $scores] );
© www.soinside.com 2019 - 2024. All rights reserved.