簡體   English   中英

我可以使用我的rails應用程序將JSON數據導入數據庫嗎?

[英]can I import JSON data into database with my rails application?

我有一個rails應用程序。 我通過Ajax調用獲得了JSON數據,現在我想將我的JSON數據導入應用程序數據庫。 我該如何存檔? 誰能幫我? 提前致謝。

---更新---
我的應用程序有一個Task模型和User模型。 用戶有很多任務,任務屬於用戶。 用戶登錄后,我將進行Ajax調用(jQuery getJSON)從另一個服務提供者獲取JSON數據。 我想將JSON數據作為任務導入數據庫。

----添加示例Json數據----

  {
   "server_time":"2010-12-22 15:27:04 +0800",
     "entries":[
       {
         "all_day":true,
         "archived":null,
         "assignment":null,
         "attribute":"plan",
         "completed":null,
         "context":null,
         "created":"2010-12-14 14:50:24 +0800",
         "deleted":null,
         "end_at":null,
         "forwarded_by":null,
         "id":"jee+ypfERGSCqlXjuyUjYw==",
         "notes":"",
         "priority":0,
         "project":null,
         "reminders":[],
         "repeat_no":null,
         "repeater":null,
         "start_at":"2010-12-14 00:00:00 +0800",
         "tags":[],
         "title":"xcv",
         "trashed":null,
         "updated":"2010-12-14 14:50:24 +0800",
         "hidden":null
       }
       ...
       {
         "all_day":true,
         "archived":null,
         "assignment":null,
         "attribute":"inbox",
         "completed":null,
         "context":null,
         "created":"2010-12-15 16:12:24 +0800",
         "deleted":null,
         "end_at":null,
         "forwarded_by":null,
         "id":"MOAvW5IBTXScMVq2WdXFXQ==",
         "notes":"",
         "priority":0,
         "project":"z1",
         "reminders":[],
         "repeat_no":null,
         "repeater":null,
         "start_at":null,
         "tags":[],
         "title":"3",
         "trashed":null,
         "updated":"2010-12-15 16:12:24 +0800",
         "hidden":null
       },
       {
         "all_day":true ,
         "archived":null,
         "assignment":null,
         "attribute":"plan",
         "completed":null,
         "context":null,
         "created":"2010-12-15 18:29:27 +0800",
         "deleted":null,
         "end_at":null,
         "forwarded_by":null,
         "id":"dSOHwcYQRbmTCO+wjtUUaQ==",
         "notes":"",
         "priority":0,
         "project":null,
         "reminders":[],
         "repeat_no":null,
         "repeater":null,
         "start_at":"2010-12-17 00:00:00 +0800",
         "tags":[],
         "title":"after day",
         "trashed":null,
         "updated":"2010-12-15 18:29:27 +0800",
         "hidden":null
       }
     ],
   "addtional":"full"
 }

如果您還沒有JSON gem,可以通過命令行安裝它:

gem install json

這將允許您將JSON解析為其等效的Ruby數據結構。

如果JSON已經與您的模型匹配,您只需要執行以下操作:

new_record = Model.new(JSON.parse(json_string_from_external_service)) 
new_record.save

如果它不匹配您的型號,您可以執行以下操作:

a_hash = JSON.parse(json_string_from_external_service)

然后你只需復制你的屬性並保存:

new_record = Model.new
new_record.attribute_1 = a_hash['key_for_attribute_1']
... etc ...
new_record.save

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM