ruby Ruby解析JSON
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby解析JSON相关的知识,希望对你有一定的参考价值。
# http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
require 'open-uri'
# https://github.com/flori/json
require 'json'
# http://stackoverflow.com/questions/9008847/what-is-difference-between-p-and-pp
require 'pp'
# Construct the URL we'll be calling
request_uri = 'http://localhost:3000/users.json'
request_query = ''
url = "#{request_uri}#{request_query}"
# Actually fetch the contents of the remote URL as a String.
buffer = open(url).read
# Sample JSON response from fake API endpoint (a simple app running on my machine), but you can easily build it. Credit goes to Michael Hartl for the stellar "Build Twitter in Rails" tutorial:
#
# http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
#
# Output after running it through http://jsonviewer.net to make it more readable.
# ['0'] (
# | id = 2
# | name = "Example User"
# | email = "example@railstutorial.org"
# | created_at = "2013-12-11T06:09:06.866Z"
# | updated_at = "2013-12-11T06:09:06.866Z"
# # ... and more
# | )
# ['1'] (
# | id = 4
# | name = "Example User 2"
# | email = "example@railstutorial.org"
# | created_at = "2013-12-11T06:09:06.866Z"
# | updated_at = "2013-12-11T06:09:06.866Z"
# # ... and more
# | )
# Convert the String response into a plain old Ruby array. It is faster and saves you time compared to the standard Ruby libraries too.
result = JSON.parse(buffer)
# An example of how to take a random sample of elements from an array. Pass the number of elements you want into .sample() method. It's probably a better idea for the server to limit the results before sending, but you can use basic Ruby skills to trim & modify the data however you'd like.
result = result.sample(5)
# Loop through each of the elements in the 'result' Array & print some of their attributes.
result.each do |user|
puts "#{user['id']}\t#{user['name']}\t#{user['email']}"
puts "Registered: #{user['created_at']}\n\n"
end
# Expected output in this format:
# Uncomment the next line to output the data inside the 5 elements returned from the JSON call. Google 'Ruby pp gem'.
# pp result
# 14 Kari Lynch example-12@railstutorial.org
# Registered: 2013-12-11T06:09:08.231Z
# 20 Dr. Josh Bergstrom example-18@railstutorial.org
# Registered: 2013-12-11T06:09:08.788Z
# 24 Haley Walter example-22@railstutorial.org
# Registered: 2013-12-11T06:09:09.157Z
# 17 Ozella Barton example-15@railstutorial.org
# Registered: 2013-12-11T06:09:08.511Z
# 13 Mandy Reynolds example-11@railstutorial.org
# Registered: 2013-12-11T06:09:08.138Z
puts "Finished!\n\n"
以上是关于ruby Ruby解析JSON的主要内容,如果未能解决你的问题,请参考以下文章