如何在Ruby on Rails中读取远程文件的内容?

时间:2021-06-22 21:11:23

here my file: http://example.com/test.txt

在这里我的文件:http://example.com/test.txt

i have to read content of http://example.com/test.txt (a JSON string) and parse it in Ruby

我必须阅读http://example.com/test.txt(一个JSON字符串)的内容并在Ruby中解析它

2 个解决方案

#1


19  

I would suggest using open-uri:

我建议使用open-uri:

require 'json'
require 'open-uri'
result = JSON.parse open('http://example.com/data.json').read

#2


4  

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.

json将包含您从uri获取的JSON字符串。

Then read this * post.

然后阅读此*帖子。

#1


19  

I would suggest using open-uri:

我建议使用open-uri:

require 'json'
require 'open-uri'
result = JSON.parse open('http://example.com/data.json').read

#2


4  

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.

json将包含您从uri获取的JSON字符串。

Then read this * post.

然后阅读此*帖子。