一尘不染

如何将JSON转换为Ruby哈希

json

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

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

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

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

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


阅读 325

收藏
2020-07-27

共1个答案

一尘不染

接下来的代码片段呢?

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