English 中文(简体)
采集 has进入开放式结构会产生“稳定的”切入
原标题:Collecting hashes into OpenStruct creates "table" entry

为什么(在铁路中评估)

[{:a => :b}].collect {|x| OpenStruct.new(x)}.to_json

添加“表”记录。

"[{"table":{"a":"b"}}]

我只想这样做:

"[{"a":"b"}]

Does it mean that Rails to_json method handles OpenStruct in a different way? When I try it in the irb, it s not there:

require  ostruct 
[{:a => :b}].collect {|x| OpenStruct.new(x)}.inspect
最佳回答

使用<代码>marshal_dump<>/code>,尽管这在一定程度上打败了将其改为“开放式结构”的目的:

[{:a => :b}].collect {|x| OpenStruct.new(x).marshal_dump }.to_json
=> "[{"a":"b"}]"

缩短的方式是:

[{:a => :b}].to_json
"[{"a":"b"}]"

或者,可以使用 mo基相机(www.roc.org) 如一夫一夫一 的 answer:

require "ostruct"
class OpenStruct
  def as_json(options = nil)
    @table.as_json(options)
  end
end
问题回答

我通过把开放式结构分类来处理这个问题:

class DataStruct < OpenStruct
  def as_json(*args)
    super.as_json[ table ]
  end
end

那么,你可以很容易地转换成像:

o = DataStruct.new(a:1, b:DataStruct.new(c:3))
o.to_json
# => "{"a":1,"b":{"c":3}}"

Neat huh? 因此,在回答你的问题时,请将此写成:

[{:a => :b}].collect {|x| DataStruct.new(x)}.to_json

你:

=> "[{"a":"b"}]"

UPDATE FOR RUBY 2.7 (Feb 5, 2021)

require  json 
require  ostruct 

class OpenStruct
  def to_json
    to_hash.to_json
  end

  def to_hash
    to_h.map { |k, v|
      v.respond_to?(:to_hash) ? [k, v.to_hash] : [k, v]
    }.to_h
  end
end

o = OpenStruct.new(a:1, b:OpenStruct.new(c:3))
p o.to_json

我发现,其他答复是令人困惑的,把我的开放结构变成了<代码>Hash或JSON。 为澄清起见,您仅可打电话到marshal_dumpOpenStruct

$ OpenStruct.new(hello: :world).to_json
=> "{"table":{"hello":"world"}}"

$ OpenStruct.new(hello: :world).marshal_dump
=> {:hello=>:world}

$ OpenStruct.new(hello: :world).marshal_dump.to_json
=> "{"hello":"world"}"

I personally would be hesitant to monkey-patch OpenStruct unless you re doing it on a subclass, as it may have unintended consequences.

2.1.2 您可以使用以下方法,在没有表层要素的情况下获得初等干事:

[{:a => :b}].collect {|x| OpenStruct.new(x).to_h}.to_json
 => "[{"a":"b"}]"
openstruct_array.map(&:to_h).as_json

这里的问题是,在内部,它采用<条码>as_json ,该编码为哈希语和表格钥匙(如:json的序号也包含该物体的所有试样变量和@table是“开放式结构”的例数,然后使用<>to_json加以说明。

因此,最容易的方法是首先使用to_h (该编码将实例变量序列化),然后使用to_json。 因此:

OpenStruct.new(x).to_h.json or in You case open_struct_array.map(&to_h.to_json





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签