问题
here my file: http://example.com/test.txt
i have to read content of http://example.com/test.txt (a JSON string) and parse it in Ruby
回答1:
I would suggest using open-uri:
require 'json'
require 'open-uri'
result = JSON.parse open('http://example.com/data.json').read
回答2:
require 'net/http'
uri = URI('http://my.json.emitter/some/action')
json = Net::HTTP.get(uri)
json
will contain the JSON string you fetched from uri
.
Then read this StackOverflow post.
来源:https://stackoverflow.com/questions/8149366/how-to-read-content-of-remote-file-in-ruby-on-rails