如何将 JSON 转换为 Ruby 哈希
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7964282/
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
How to convert JSON to a Ruby hash
提问by verdure
I have a JSON object holding the following value:
我有一个包含以下值的 JSON 对象:
@value = {"val":"test","val1":"test1","val2":"test2"}
I want to loop through it in Ruby to get the key/value pairs. When I use @each, it doesn't iterate through the object because it is not in the Ruby hash form:
我想在 Ruby 中遍历它以获取键/值对。当我使用 时@each,它不会遍历对象,因为它不是 Ruby 散列形式:
@value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}
How can I convert the above JSON object to a Ruby hash?
如何将上述 JSON 对象转换为 Ruby 哈希?
回答by WarHog
What about the following snippet?
下面的片段怎么样?
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}
回答by crims345
You could also use Rails' with_indifferent_accessmethod so you could access the body with either symbols or strings.
您还可以使用 Rails 的with_indifferent_access方法,以便您可以使用符号或字符串访问正文。
value = '{"val":"test","val1":"test1","val2":"test2"}'
json = JSON.parse(value).with_indifferent_access
then
然后
json[:val] #=> "test"
json["val"] #=> "test"
回答by Nick Cartwright
Have you tried: http://flori.github.com/json/?
你有没有试过:http: //flori.github.com/json/?
Failing that, you could just parse it out? If it's only arrays you're interested in, something to split the above out will be quite simple.
失败了,你可以把它解析出来吗?如果它只是您感兴趣的数组,那么将上述内容拆分出来将非常简单。
回答by boulder_ruby
Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your JSON hash contents in %q{hsh}tags.
假设您有一个 JSON 散列在某处,要自动将其转换为类似 WarHog 的版本,请将您的 JSON 散列内容包装在%q{hsh}标签中。
This seems to automatically add all the necessary escaped text like in WarHog's answer.
这似乎会自动添加所有必要的转义文本,就像 WarHog 的回答一样。
回答by the Tin Man
I'm surprised nobody pointed out JSON's []method, which makes it very easy and transparent to decode and encode from/to JSON.
我很惊讶没有人指出 JSON 的[]方法,这使得从/到 JSON 解码和编码变得非常容易和透明。
If object is string-like, parse the string and return the parsed result as a Ruby data structure. Otherwise generate a JSON text from the Ruby data structure object and return it.
如果 object 是类似字符串的,则解析字符串并将解析结果作为 Ruby 数据结构返回。否则从 Ruby 数据结构对象生成一个 JSON 文本并返回它。
Consider this:
考虑一下:
require 'json'
hash = {"val":"test","val1":"test1","val2":"test2"} # => {:val=>"test", :val1=>"test1", :val2=>"test2"}
str = JSON[hash] # => "{\"val\":\"test\",\"val1\":\"test1\",\"val2\":\"test2\"}"
strnow contains the JSON encoded hash.
str现在包含 JSON 编码的hash.
It's easy to reverse it using:
使用以下方法很容易反转它:
JSON[str] # => {"val"=>"test", "val1"=>"test1", "val2"=>"test2"}
Custom objects need to_sdefined for the class, and inside it convert the object to a Hash then use to_jsonon it.
需要to_s为类定义自定义对象,并在其中将对象转换为 Hash 然后to_json在其上使用。
回答by Mario Ruiz
You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash
您可以使用 nice_hash gem:https: //github.com/MarioRuiz/nice_hash
require 'nice_hash'
my_string = '{"val":"test","val1":"test1","val2":"test2"}'
# on my_hash will have the json as a hash, even when nested with arrays
my_hash = my_string.json
# you can filter and get what you want even when nested with arrays
vals = my_string.json(:val1, :val2)
# even you can access the keys like this:
puts my_hash._val1
puts my_hash.val1
puts my_hash[:val1]

