【问题标题】:Rails rabl json formatRails rabl json 格式
【发布时间】:2013-04-06 17:30:31
【问题描述】:

我正在使用 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
      }
   }
]

我需要我的 json 格式如下:

{
    "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

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

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    我是这样做的:

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

    也许这是个坏主意?

    【讨论】:

    • 来自回滚的编辑评论:它必须是“banks”,而不是“bank”才能匹配他想要的格式。
    猜你喜欢
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多