【问题标题】:Nest JSON into "data"将 JSON 嵌套到“数据”中
【发布时间】:2019-02-07 09:55:54
【问题描述】:

我目前以这种格式返回 JSON 到 ajax 调用:
[
{"username":"test3","points":62,"sportName":"Basketball"},
{"username":"test2","points":55,"sportName":"Football"}
]

但我想要这样:
{
“数据”:
[
{“用户名”:“test3”,“积分”:“62”,“运动名”:“篮球”},
{“用户名”:” test2", "points":"55", "sportName":"Football" }
]
}

返回它的函数(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);

【问题讨论】:

  • 请使用 Snippet [<>] 按钮及其Tidy 选项来格式化您的代码。
  • 你需要字符串points吗?

标签: php json laravel


【解决方案1】:

你可以简单地做到这一点。

return json_encode( ['data' => $scores] );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2019-04-11
    相关资源
    最近更新 更多