2015-05-24 74 views
0

山魈JSON数据发送回我的网页钩时转换为Ruby的数据结构,它看起来像下面:转换JSON - >红宝石哈希散列的数组

{ "image.jpg" => { "name => "image.jpg", "type" => "image/jpeg", "content" => "", "base64" => true } } 

他们送这一点,当我需要的是一组哈希,例如:

[{ "name => "image.jpg", "type" => "image/jpeg", "content" => "", "base64" => true }] 

第一组数据如何转换为散列数组?

回答

1

尝试返回的数据设置为foo:

foo = { "image.jpg" => { "name" => "image.jpg", "type" => "image/jpeg", "content" => "", "base64" => true } } 

然后做:

Array.wrap(foo["image.jpg"]) 

而且,你错过了你的哈希

的第一个“姓名”键后结束引号符号

编辑:您可以只设置到foo然后运行:

foo.values 
+0

它需要是动态的,不能静态使用“image.jpg”,因为它会有所不同。 – Noah