是否可以将 JSON 字符串转换为对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13897846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to convert a JSON string to an object?
提问by mickael
Possible Duplicate:
Parsing a JSON string in ruby
可能的重复:
在 ruby 中解析 JSON 字符串
Is it possible to convert a JSON string into a Ruby object? I would like to access its information with an expression similar to:
是否可以将 JSON 字符串转换为 Ruby 对象?我想使用类似于以下的表达式访问其信息:
drawer.stations.tv.header
JSON string:
JSON 字符串:
{
"drawer" : {
"stations" : {
"tv" : {
"header" : "TV Channels",
"logos" : {
"one" : "www1",
"two" : "www2",
"three" : "www3"
}
}
}
}
}
回答by Sergio Tulentsev
You can parse the string into a ruby hash and then turn it into a Mash. Mash provides you with method-like access.
您可以将字符串解析为 ruby 哈希,然后将其转换为Mash。Mash 为您提供了类似方法的访问。
require 'json'
require 'hashie'
hash = JSON.parse json_string
obj = Hashie::Mash.new hash
obj.drawer.stations.tv.header # => "TV Channels"
Update
更新
You can also do it without a 3rd party gem, using ruby's own OpenStruct:
你也可以在没有 3rd party gem 的情况下使用 ruby 自己的OpenStruct:
require 'ostruct'
require 'json'
obj = JSON.parse(json_string, object_class: OpenStruct)
obj.drawer.stations.tv.header # => "TV Channels"
回答by Richie Min
if your parse this string to ruby object, it will return a ruby Hash object, you can get it like this
如果您将此字符串解析为 ruby 对象,它将返回一个 ruby Hash 对象,您可以像这样得到它
ruby_obj = JSON.parse(json_string)
ruby_obj['drawer']['stations']['tv']['header']
回答by BernardK
require 'json'
json_info = %q(
{
"drawer" : {
"stations" : {
"tv" : {
"header" : "TV Channels",
"logos" : {
"one" : "www1",
"two" : "www2",
"three" : "www3"
}
}
}
}
}
)
class MyJson
def self.for(p_jason_string)
self.new(JSON.parse(p_jason_string))
end
def initialize(p_info)
@info = p_info
end
def inspect
@info.inspect
end
def method_missing(p_missing_method_name)
print 'mm '; p p_missing_method_name
key = p_missing_method_name.to_s
if @info.has_key?(key)
then
MyJson.new(@info[key])
else
puts "no key #{p_missing_method_name}"
end
end
end # class MyJson
holder = MyJson.for(json_info)
puts '-----holder.drawer'
p holder.drawer
puts '-----holder.drawer.stations'
p holder.drawer.stations
puts '-----holder.drawer.stations.tv.header'
p holder.drawer.stations.tv.header
Execution :
执行 :
$ ruby -v
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.2.0]
$ ruby -w t.rb
-----holder.drawer
mm :drawer
{"stations"=>{"tv"=>{"header"=>"TV Channels", "logos"=>{"one"=>"www1", "two"=>"www2", "three"=>"www3"}}}}
-----holder.drawer.stations
mm :drawer
mm :stations
{"tv"=>{"header"=>"TV Channels", "logos"=>{"one"=>"www1", "two"=>"www2", "three"=>"www3"}}}
-----holder.drawer.stations.tv.header
mm :drawer
mm :stations
mm :tv
mm :header
"TV Channels"
Note that I use RVM and have done nothing special to have json working, must have been automatically installed.
请注意,我使用 RVM 并没有做任何特别的事情来让 json 工作,肯定是自动安装的。

