包括JSON数组根源在哪里? [英] include root in json for array?
本文介绍了包括JSON数组根源在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
3导轨:include_root_in_json = TRUE允许有JSON响应根属性。但它不与物体的阵列的工作,有像include_root_in_array_json一个属性?
其实我有这样的回应:
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响应具有root属性模型,其中包括数组,但不是JSON ...
xml response has a root property 'models' which include the array, but json not...
推荐答案
您可以试试:
{models: Model.all}.to_json
这不是一个非常优雅的解决方案,但它的工作的罚款。
It's not a very elegant solution but it does its job fine.
这篇关于包括JSON数组根源在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文