在 Ruby 中解析 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5410682/
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
Parsing a JSON string in Ruby
提问by Rod
I have a string that I want to parse in Ruby:
我有一个要在 Ruby 中解析的字符串:
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
Is there an easy way to extract the data?
有没有简单的方法来提取数据?
回答by Greg
This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g. json_string, like so:
这看起来像JavaScript Object Notation (JSON)。您可以解析驻留在某个变量中的 JSON,例如json_string,像这样:
require 'json'
JSON.parse(json_string)
If you're using an older Ruby, you may need to install the json gem.
如果您使用的是较旧的 Ruby,则可能需要安装json gem。
There are also other implementations of JSON for Ruby that may fit some use-cases better:
还有其他 JSON 的 Ruby 实现可能更适合某些用例:
回答by nevan king
Just to extend the answers a bit with what to do with the parsed object:
只是为了对解析的对象做些什么来扩展答案:
# JSON Parsing example
require "rubygems" # don't need this if you're Ruby v1.9.3 or higher
require "json"
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
parsed = JSON.parse(string) # returns a hash
p parsed["desc"]["someKey"]
p parsed["main_item"]["stats"]["a"]
# Read JSON from a file, iterate over objects
file = open("shops.json")
json = file.read
parsed = JSON.parse(json)
parsed["shop"].each do |shop|
p shop["id"]
end
回答by dav_i
回答by keymone
It looks like a JSON string. You can use one of many JSON libraries and it's as simple as doing:
它看起来像一个 JSON 字符串。您可以使用许多 JSON 库之一,它就像这样做一样简单:
JSON.parse(string)
回答by guy mograbi
This is a bit late but I ran into something interesting that seems important to contribute.
这有点晚了,但我遇到了一些有趣的事情,这似乎很重要。
I accidentally wrote this code, and it seems to work:
我不小心写了这段代码,它似乎有效:
require 'yaml'
CONFIG_FILE = ENV['CONFIG_FILE'] # path to a JSON config file
configs = YAML.load_file("#{CONFIG_FILE}")
puts configs['desc']['someKey']
I was surprised to see it works since I am using the YAML library, but it works.
我很惊讶地看到它有效,因为我使用的是 YAML 库,但它有效。
The reason why it is important is that yamlcomes built-in with Ruby so there's no gem install.
之所以重要,是因为它是yamlRuby 内置的,因此无需安装 gem。
I am using versions 1.8.x and 1.9.x - so the jsonlibrary is not built in, but it is in version 2.x.
我使用的是 1.8.x 和 1.9.x 版 - 所以json库不是内置的,但它是 2.x 版。
So technically - this is the easiest way to extract the data in version lower than 2.0.
所以从技术上讲 - 这是在低于 2.0 的版本中提取数据的最简单方法。
回答by damianmr
I suggest Oj as it is waaaaaay faster than the standard JSON library.
我建议使用 Oj,因为它比标准 JSON 库快 waaaaaay。
回答by Justin Ethier
That data looks like it is in JSON format.
该数据看起来像是 JSON 格式。
You can use this JSON implementation for Rubyto extract it.
您可以使用此Ruby 的 JSON 实现来提取它。

