繁体   English   中英

ActiveModel序列化程序中的动态根密钥

[英]Dynamic root Key in ActiveModel Serializers

我在我的项目中使用Rails 5Acive Model Serializer (0.10.6) 我需要以下格式的JSON

{
   "account_lists": {
    "0": {
            "id": 1,
            "description": "test tets test tets"
          },
     "1": {
            "id": 2,
            "description": "test tets test tets"
          }
      }
}

01键将是对象的索引。

我尝试使用下面的代码,但结果不会到来

account_serializer.rb

class AccountSerializer < ActiveModel::Serializer
  attributes :custom_method

  def custom_method
    { object.id => {id: object.id, description: object. description } }
  end
end

accounts_controller.rb

render json: @accounts, root: "account_lists", adapter: :json

结果是

{
"account_lists": [
    {
        "custom_method": {
            "46294": {
                "id": 46294,
                "description": "test tets test tets"
            }
        }
    },
    {
        "custom_method": {
            "46295": {
                "id": 46295,
                "description": "test tets test tets"
            }
        }
   ]
 }

你能帮我拿到结果吗?

您可以通过两种方式实现此目的,使用序列化程序或不使用序列化程序。

为了使这与串行器一起工作,我们需要编写一些元编程。 这样的事情

class AccountSerializer < ActiveModel::Serializer
    attributes

    def initialize(object, options = {})
        super
        self.class.send(:define_method, "#{object.id}") do
            {
                "#{object.id}": {
                    id: object.id,
                    description: object.description
                }
            }
        end
    end

    def attributes(*args)
        hash = super
        hash = hash.merge(self.send("#{object.id}"))
    end
end

这将产生这样的结果

{
    "account_lists": [
        {
          "19":{
              "id":19,
              "description":"somethign here "
            }
        },
        {
            "20":{
                "id":20,
                "description":"somethign here "
            }
        },{
            "48":{
                "id":48,
                "description":"somethign here"
            }
        }
    ]
}

如果你想让json回复你所提到的问题,你最好选择一个为你构建json的自定义库类

class BuildCustomAccountsJson

  def initialize(accounts)
    @accounts = accounts
  end

  def build_accounts_json(accounts)
    _required_json = {}

    @accounts.map do |account|
     _required_json["#{account.id}"] = { id: "#{account.id}", description: "#{account.description}" }
    end

    _required_json
  end

end

在控制器动作中,我们可以使用这个类

def index
  ...
  ...

  @accounts_json = BuildCustomAccountsJson.new(@accounts).build_accounts_json

  render json: @accounts_json, root: "accounts_list"
end 

这将产生像这样的json

{
   "account_lists": {
    "0": {
            "id": 1,
            "description": "test tets test tets"
          },
     "1": {
            "id": 2,
            "description": "test tets test tets"
          }
      }
}

就这样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM