如何连接到单独的服务器并解析其 JSON 响应? [英] How can i connect to a separate server and parse its JSON response?
本文介绍了如何连接到单独的服务器并解析其 JSON 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要连接到我们管理的另一台服务器,并让 rails 处理它的结果(以 JSON 格式)...我该怎么做?
i need to connect to another server we manage and have it's results (in JSON format) processed by rails... how can I do it ?
谢谢!
推荐答案
使用 NET/HTTP 库,可以获取远程文件的内容.
然后使用 JSON 库,您可以将 json 字符串解析为哈希.
With the NET/HTTP library, you can get the content of a distant file.
Then with the JSON library you can parse a json string into a hash.
def get_datas
url = URI.parse("http://www.example.com/page.json")
res = Net::HTTP.start(url.host, url.port) {|http|
http.get(url.path)
}
JSON.parse res.body
end
这篇关于如何连接到单独的服务器并解析其 JSON 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文