我有一个持有以下值的JSON对象:
@value = {"val":"test","val1":"test1","val2":"test2"}
我想在Ruby中循环遍历以获得键/值对。当我使用时@each,它不会遍历对象,因为它不是Ruby哈希形式:
@each
@value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}
如何将上述JSON对象转换为Ruby哈希?
接下来的代码片段呢?
require 'json' value = '{"val":"test","val1":"test1","val2":"test2"}' puts JSON.parse(value) # => {"val"=>"test","val1"=>"test1","val2"=>"test2"}