所以我有一个非常简单的 rabl 视图,它将支持 xml 和 json (curr.rabl) 的输出:
collection @currencies => "currencies"
attributes :code, :name
我的 rabl 配置:
Rabl.configure do |config|
config.include_json_root = false
config.include_child_root = false
config.xml_options = { :skip_types => true, :camelize => :lower }
config.include_xml_root = false
end
rabl 为 xml 和 json 提供的输出:
XML:
<currencies>
<currency>
<code>AFN</code>
<name>Afghan Afghani</name>
</currency>
<currency>
<code>AFA</code>
<name>Afghanistan Afghani</name>
</currency>
</currencies>
JSON:
{
currencies: [
{
code: "AFN",
name: "Afghan Afghani"
},
{
code: "AFA",
name: "Afghanistan Afghani"
}]}
对我来说,这是不正确的。根据我一直在阅读的内容,JSON 应该如下所示:
{
currencies: {currency: [
{
code: "AFN",
name: "Afghan Afghani"
},
{
code: "AFA",
name: "Afghanistan Afghani"
} ] }
}
如果我设置config.include_child_root(/include_json_root) = true
,那么我得到以下(仍然不正确)
{
currencies: [
{
currency: {
code: "AFN",
name: "Afghan Afghani"
}
},
{
currency: {
code: "AFA",
name: "Afghanistan Afghani"
} } ] }
首先,我对 RABL 输出错误 json 的假设是否正确?其次,有没有办法让 RABL 为这个视图做我想要的,以及任何其他可能有许多嵌套子对象的视图?