如何将JSON转换为Ruby哈希 [英] How to convert JSON to a Ruby hash

查看:57
本文介绍了如何将JSON转换为Ruby哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有以下值的JSON对象:

I have a JSON object holding the following value:

@value = {"val":"test","val1":"test1","val2":"test2"}

我想在Ruby中遍历它以获得键/值对.当我使用@each时,它不会遍历该对象,因为它不是Ruby哈希形式:

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:

@value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}

如何将上述JSON对象转换为Ruby哈希?

How can I convert the above JSON object to a Ruby hash?

推荐答案

以下代码段如何?

require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}

这篇关于如何将JSON转换为Ruby哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆