我有一个要在 Ruby 中解析的字符串:
string = '{"desc":{"someKey":"someValue","anotherKey":"value"},"main_item":{"stats":{"a":8,"b":12,"c":10}}}'
有没有简单的方法来提取数据?
最佳答案
这看起来像 JavaScript Object Notation (JSON) .您可以解析驻留在某个变量中的 JSON,例如json_string
,像这样:
require 'json'
JSON.parse(json_string)
如果您使用的是旧版 Ruby,您可能需要安装 json gem .
还有其他适用于 Ruby 的 JSON 实现可能更适合某些用例:
关于ruby - 在 Ruby 中解析 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5410682/