将json转换为ruby hash [英] Convert json to ruby hash

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

问题描述

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

我想通过它循环在Ruby中获取键值对。当我使用 @each 时,它不会遍历该对象,因为它不在ruby哈希表中:

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

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

解决方案

下面的代码片段如何?

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


I have a JSON object holding the following value:

@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:

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

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

解决方案

What about the following snippet?

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

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

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