从url中获取json对象,同时在rails上的ruby中使用facebook graph api

Anu*_*rag 8 ruby json ruby-on-rails

如何通过我的ruby on rails代码中的URL获取JSON对象:

url = "https://graph.facebook.com/me/friends?access_token=XXX"
Run Code Online (Sandbox Code Playgroud)

我无法在rails上获得JSON + Ruby的优秀教程.有人可以帮我谢谢.

M. *_*her 27

require 'open-uri'
require 'json'

json_object = JSON.parse(open("https://graph.facebook.com/me/friends?access_token=XXX").read)
Run Code Online (Sandbox Code Playgroud)

这是最简单的方法,虽然它可能不是最快的.

编辑:如果URI的参数字符串中包含特殊字符,则必须首先对其进行转义/编码URI.escape.

  • open-uri是ruby标准库的一部分 (4认同)