简体   繁体   中英

converting ruby hash into json object

So I am iterating through a set of data and building a hash from it:

clean_response = Array.new
        response.each_with_index do |h, idx|
        clean_response <<
        {
                    :lat => h["location"]["latitude"],
                    :lg => h["location"]["longitude"],
                    :place => h["location"]["name"],
                    #This grabs the entire hash at "location" because we are wanting all of that data
                    :profile_picture => h["user"]["profile_picture"],
                    :hash_tags => h["tags"],
                    :username => h["user"]["username"],
                    :fullname => h["user"]["full_name"],
                    :created_time => (Time.at(h["created_time"].to_i)).to_s,
                    :image => h["images"]["low_resolution"]["url"] # we can replace this with whichever resolution.
        }
        end

Which return an array of hashes like so:

[{:lat=>40.7486382,
  :lg=>-73.9487686,
  :place=>"The Cliffs at LIC",
  :profile_picture=>"http://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/12104940_1653775014895036_286845624_a.jpg",
  :hash_tags=>["bouldering"],
  :username=>"denim_climber",
  :fullname=>"DenimClimber",
  :created_time=>2015-10-13 22:58:09 -0400,
  :image=>"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11856571_1062082890510188_611068928_n.jpg"},
 {:lat=>40.7459602,
  :lg=>-73.9574966,
  :place=>"SHI",
  :profile_picture=>"http://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/11348212_1453525204954535_631200718_a.jpg",
  :hash_tags=>["cousins", "suchafunmoment", "johnlennonstyle"],
  :username=>"xiomirb",
  :fullname=>"Xiomi",
  :created_time=>2015-10-13 22:57:21 -0400,
  :image=>"https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e35/11375290_1688934151392424_2009781937_n.jpg"}]

I'd like to convert this data to json and then serve it to a specific view. How can I convert this? I tried the .to_json method but it doesn't return a well formatted one since my UI isn't binding to the data.

You can convert a Ruby hash into JSON using to_json :

require 'json'

your_hash.to_json # gives you a JSON object

But, in your case the data is an array of hashes, but NOT a hash. So, your to_json would not work.

I am not quite sure how you want to do this, but one possibility is to loop through the array of hashes, get each hash and convert that to a JSON object using to_json call (like shown above) and build a new array of JSON objects. This way, you can build an array of JSON objects from an array of hashes.

array_of_json = []
# loop through the array of hashes
clean_response.each do |hash|
  array_of_json << hash.to_json
end
array_of_json # array of JSON objects

If by "serve it to a specific view" you mean pass it to a .haml or .erb template, you can pass the array of hashes as is. Both haml and erb will allow you to iterate over the array, and even the hash if you want.

If you mean you want to hand a json string to the browser, #to_json should work fine. Other options are jbuilder or oat when you want to refine what is sent, but to_json should "serve" you well!

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