0

所以我有一个非常简单的 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 为这个视图做我想要的,以及任何其他可能有许多嵌套子对象的视图?

4

2 回答 2

1

需要明确的是,这绝对不是标准的:

{
currencies: {currency: [
{
code: "AFN",
name: "Afghan Afghani"
},
{
code: "AFA",
name: "Afghanistan Afghani"
} ] }
}

RABL 产生的两种形式都是标准的。一无根,一有。上面这件事没有任何意义,我从未见过任何 API 那样做。

于 2012-11-28T03:30:43.733 回答
0

我总是使用上面的第二种形式:

{
   currencies: [
   {
      currency: {  ... },
   },
   { 
      currency: { ... }
   }]
}

如果你真的想这样做:

{
   currencies: { currency: [ { ... }, { ... }] }
}

我认为您可以将 include_json_root 设置为 false 并使用自定义节点,但它可能会变得复杂。

于 2012-11-27T21:24:10.520 回答