RABL的JSON输出是否不符合标准?它可以? [英] Does RABL's JSON output not conform to standard? Can it?

查看:101
本文介绍了RABL的JSON输出是否不符合标准?它可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个非常简单的rabl视图,该视图将同时支持xml和json(curr.rabl)的输出:

So I have a very simple rabl view that will support output for both xml and 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提供的输出:

Output that rabl gives for xml and 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应该类似于以下内容:

To me, this is not correct. The JSON should look like the following according to what I've been reading:

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

如果设置了config.include_child_root(/include_json_root) = true,则得到以下信息(仍然不正确)

If I set the config.include_child_root(/include_json_root) = true, then I get the following (still not correct)

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

首先,在RABL输出错误的json的假设中,我是否正确? 其次,有没有办法让RABL对此视图以及可能有许多嵌套子对象的任何其他视图执行我想做的事情?

First, am I correct in my assumption that RABL is outputting the wrong json? Second, is there a way to get RABL to do what I want for this view, as well as any other view that may have many nested child objects?

推荐答案

请注意,这绝对是非标准:

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

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

Both forms RABL produces are standard. One without root and one with. This thing above doesn't make any sense, I have never seen any API act that way.

这篇关于RABL的JSON输出是否不符合标准?它可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆