简体   繁体   中英

Convert JSON -> Ruby Hash to Array of Hashes

Mandrill sends back JSON data to my web-hook and when converted to Ruby data structures it looks like the below:

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

They send this, when what I need is an Array of Hashes, ex:

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

How can the first set of data be converted to an array of hashes?

Try setting the returned data to foo:

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

Then do:

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

Also, you're missing a closing quote symbol after the first "name" key in your hash

Edit: You can just set it to foo then run:

foo.values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM