【问题标题】:Dynamic root Key in ActiveModel SerializersActiveModel 序列化程序中的动态根键
【发布时间】:2018-04-04 01:31:34
【问题描述】:

我在我的项目中使用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"
          }
      }
}

0,1 键是对象的索引。

我尝试了下面的代码,但结果不出来

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"
            }
        }
   ]
 }

你能帮我得到结果吗?

【问题讨论】:

  • 如果不需要root,可以将root设置为false。像这样render json: @accounts, root: false, adapter: :json
  • @SujayGavhane 我需要根目录,但我想要对象根目录的自定义根目录。
  • @Jenorish 你知道了吗,我只需要像这样工作。
  • @7urkm3n 还没试过。
  • @Jenorish 你遇到什么样的错误?

标签: ruby-on-rails serialization ruby-on-rails-5 active-model-serializers


【解决方案1】:

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

为了让序列化器能够工作,我们需要编写一些元编程。像这样的

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

就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2019-05-01
    • 2016-10-03
    • 2018-07-04
    • 2019-05-21
    相关资源
    最近更新 更多