1

对不起,我英语不好。

我正在尝试找到与我的问题相同的主题。我的问题是 json 而不是 root。

这是我的 json 数据:

[
{
created_at: "2013-07-26T02:49:55Z",
description: "hgfhgfhf",
iconapp: null,
id: 13,
name: "test1",
updated_at: "2013-07-26T02:49:55Z",
},
{
created_at: "2013-07-26T02:54:40Z",
description: "sadsadas",
iconapp: null,
id: 14,
name: "asdadas",
updated_at: "2013-07-26T02:54:40Z",
}
]

但我希望我的 json 数据有这样的根元素:

- Apps: [
{
created_at: "2013-07-26T02:49:55Z",
description: "hgfhgfhf",
iconapp: null,
id: 13,
name: "test1",
updated_at: "2013-07-26T02:49:55Z",
},
{
created_at: "2013-07-26T02:54:40Z",
description: "sadsadas",
iconapp: null,
id: 14,
name: "asdadas",
updated_at: "2013-07-26T02:54:40Z",
}
]

Json root 是“应用程序”。我该怎么办?

请帮帮我 ?

4

1 回答 1

1

它是一个配置的东西。默认情况下,根不包含在 json 中。您可以通过以下方式启用它:

ActiveRecord::Base.include_root_in_json = true

如果您希望它在应用程序范围内添加,请将其添加到新的初始化程序中,该初始化程序将在应用程序启动时运行代码。它记录在这里:http ://apidock.com/rails/ActiveRecord/Serialization/to_json

于 2013-08-02T09:21:19.633 回答