Rails rabl json格式

时间:2013-04-06 17:30:31

标签: ruby-on-rails json rabl

我正在使用rabl以json格式显示我的数据库中的数据来解析它的android ... 我的json看起来像这样:

[
   {
      "bank":{
         "central_office_address":"ololo",
         "license":"12312312",
         "location_id":3,
         "name":"Pbank",
         "tax_number":"12312312",
         "year_of_foundation":1987
      }
   },
   {
      "bank":{
         "central_office_address":"sdfsdf sdf",
         "license":"321312",
         "location_id":3,
         "name":"Bbank",
         "tax_number":"321321",
         "year_of_foundation":1999
      }
   }
]

我需要的格式如下:

{
    "contacts": [
        {
                "id": "c200",
                "name": "Ravi Tamada",
                "email": "ravi@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        },
        {
                "id": "c201",
                "name": "Johnny Depp",
                "email": "johnny_depp@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
    ]
}

此类数据通常在java ....中解析。

我的拉布尔观点:

object @banks

attributes :central_office_address, :license, :location_id, :name, :tax_number, :year_of_foundation

如何更改其输出以匹配第二个示例?

1 个答案:

答案 0 :(得分:1)

我这样做了:

collection @banks, :root => "bank", :object_root => false
attributes :central_office_address, :license, :location_id, :name, :tax_number, :year_of_foundation

也许这是个坏主意?