include root in json for array?

若如初见. 提交于 2019-12-22 20:08:26

问题


rails 3: include_root_in_json = true allows to have a root property for json response. But it doesn't work with array of object, there is a property like include_root_in_array_json? actually I have this response:

[ {"model": {"a":"a_value"}}, {"model": {"b":"b_value"}}]

but I want a response like that:

{"models":[ {"model": {"a":"a_value"}}, {"model": {"b":"b_value"}}]}

xml response has a root property 'models' which include the array, but json not...


回答1:


You can try:

{models: Model.all}.to_json

It's not a very elegant solution but it does its job fine.



来源:https://stackoverflow.com/questions/9750690/include-root-in-json-for-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!