如何使API调用来从URL获取JSON数据

问题描述:

我试图从一个url我的Rails应用程序获取一个JSON数据。如何使这个API调用。

i am trying to fetch a json data in my rails app from a url. how to make this api call.

 {
  "name": "MagicList",
  "count": 20,
  "frequency": "realtime",
  "version": 5,
  "newdata": true,
  "lastrunstatus": "success",
  "lastsuccess": "Sat Apr 26 2014 14:34:40 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "Title": {
          "href": "http://www.magicbricks.com/propertyDetails/2-BHK-746-Sq-ft-Multistorey-Apartment-FOR-Sale-Sector-79-in-Gurgaon&id=C0gt4k2Fa9tzpSvf+uAgZw==",
          "text": "2 BHK Multistorey Apartment in Sector-79"
        },
        "Price": "37.99 Lac(s)",
        "Rate": "5,092",
        "Size": "746 Sq-ft",
        "Project Name": "",
        "New_Resale": "New Property"
      },
      {
        "Title": {
          "href": "http://www.magicbricks.com/propertyDetails/2-BHK-1050-Sq-ft-Multistorey-Apartment-FOR-Sale-Sector-68-in-Gurgaon&id=M5jckQCuJaBzpSvf+uAgZw==",
          "text": "2 BHK Multistorey Apartment in Sector-68"
        },
        "Price": "57.50 Lac(s)",
        "Rate": "5,476",
        "Size": "1050 Sq-ft",
        "Project Name": "Project: Pareena Sec68",
        "New_Resale": "New Property"
      },
      {
        "Title": {
          "href": "http://www.magicbricks.com/propertyDetails/3-BHK-1875-Sq-ft-Multistorey-Apartment-FOR-Sale-Sector-37C-in-Gurgaon&id=gw8zjmcA/b1zpSvf+uAgZw==",
          "text": "3 BHK Multistorey Apartment in Sector-37C"
        },
        "Price": "60.0 Lac(s)\nOnwards",
        "Rate": "3,200",
        "Size": "1875 Sq-ft",
        "Project Name": "Project: Spire Greens",
        "New_Resale": "Resale"
      }
     }
   }

不知道它是如何实现的,应该使用哪些宝石。

dont know how it can be achieved, which gem should be used.

使用开放-URI

url = "http://..."
data = JSON.parse(open(url).read)

或者用网

require "net/http"

url = "http://..."
resp = Net::HTTP.get_response(URI.parse(url))
data = JSON.parse(resp.body)