转换JSON - > Ruby散列哈希数组

时间:2015-05-24 23:00:48

标签: arrays ruby hash

Mandrill将JSON数据发送回我的web-hook,当转换为Ruby数据结构时,它如下所示:

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

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

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

如何将第一组数据转换为哈希数组?

1 个答案:

答案 0 :(得分:2)

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

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

然后做:

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

此外,您在哈希

中的第一个“name”键后缺少结束引号

编辑:您可以将其设置为foo然后运行:

foo.values