Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RABL';s JSON输出不符合标准?可以吗?_Ruby On Rails_Json_Rabl - Fatal编程技术网

Ruby on rails RABL';s JSON输出不符合标准?可以吗?

Ruby on rails RABL';s JSON输出不符合标准?可以吗?,ruby-on-rails,json,rabl,Ruby On Rails,Json,Rabl,因此,我有一个非常简单的rabl视图,它将支持xml和json(curr.rabl)的输出: 我的rabl配置: Rabl.configure do |config| config.include_json_root = false config.include_child_root = false config.xml_options = { :skip_types => true, :camelize => :lower } config.incl

因此,我有一个非常简单的rabl视图,它将支持xml和json(curr.rabl)的输出:

我的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:

JSON:

对我来说,这是不正确的。根据我所阅读的内容,JSON应该如下所示:

{
currencies: {currency: [
{
code: "AFN",
name: "Afghan Afghani"
},
{
code: "AFA",
name: "Afghanistan Afghani"
} ] }
}
如果我设置了
config.include\u child\u root(/include\u json\u root)=true
,那么我会得到以下结果(仍然不正确)

首先,我认为RABL输出的json错误,这是否正确?
第二,有没有办法让RABL为这个视图以及可能有许多嵌套子对象的任何其他视图执行我想要的操作?

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

{
   currencies: [
   {
      currency: {  ... },
   },
   { 
      currency: { ... }
   }]
}
如果您真的想这样做:

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

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

只是想澄清一下,这肯定是非标准的

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

RABL生产的两种形式都是标准的。一个没有根,一个有根。上面这件事没有任何意义,我从未见过任何API以这种方式运行。

我总是使用上面的第二种形式。你有一个货币集合(数组),而不是一个散列,一个节点(货币)就是一个数组。我实际上同意你的观点,但似乎所有的xml到JSON转换器都是按照我上面提到的方式在线完成的。请参阅,两者都是有效的JSON结构。您知道哪个客户端将使用JSON吗?XML->JSON转换是有意义的,但这并不一定意味着它就是您想要的。
{
currencies: [
{
currency: {
code: "AFN",
name: "Afghan Afghani"
}
},
{
currency: {
code: "AFA",
name: "Afghanistan Afghani"
} } ] }
{
   currencies: [
   {
      currency: {  ... },
   },
   { 
      currency: { ... }
   }]
}
{
   currencies: { currency: [ { ... }, { ... }] }
}
{
currencies: {currency: [
{
code: "AFN",
name: "Afghan Afghani"
},
{
code: "AFA",
name: "Afghanistan Afghani"
} ] }
}