我正在尝试使用RABL生成JSON,我的index.rabl
视图如下所示:
collection @products => :products
attributes :id, :name, :price, :category_id
node(:total) {@products.count}
这会生成具有以下结构的JSON:
{"products":[{"id":1,"name":"product name","price":0.00,"category_id":1,"total":30},
{"id":2,"name":"product2 name","price":0.00,"category_id":1,"total":30},...]}
但是,我希望生成的JSON的结构如下所示:
{ "products":[{"id":1,"name":"product name","price":0.00,"category_id":1},
{"id":2,"name":"product2 name","price":0.00,"category_id":1},...],
"total":30
}
这意味着我希望从"total":30
数组中获取"products"
并将其放入生成的JSON的根对象中。我需要对视图文件进行哪些更改才能生成所需的JSON?我对RABL的经验很少,我们将非常感谢帮助。
答案 0 :(得分:1)
在 index.json.rabl 中,您可以执行以下操作:
object false
child @events do
attributes :id, :message
end
node(:count) { @events.size }
结果是:
{
"count": 50,
"events": [
{
"id": 124,
"message": "Hola"
},
{
"id": 123,
"message": "Chau"
},
{
"id": 122,
"message": "Yeah baby!"
}
]
}
我在我的项目上测试了这个。您可以使用产品更改活动......