2015-11-23 25 views
0

我想存儲存儲在某個url的json數據。如何做到這一點?如何通過URL在RoR中填充數據庫?

+2

使用像'httparty'或'faraday'這樣的gem與url進行通信,並以json格式從它獲取數據,然後解析在'http response'中收到的數據並存儲它因此。 –

回答

0

像上面的評論,我會推薦一個像httparty寶石。您可以發送獲取請求到所需的網址,例如response = HTTParty.get('www.exampleurl.com'),並使用rails json解析gem/feature來分析它並從中創建一條記錄。但總結一下:

include 'httparty' 

response = HTTParty.get('www.exampleurl.com') 
json_response = JSON.parse(response) 

json_response.each do |json| 
    Object.create(name: json.name, some_field: json.some_field) 
end