2011-03-17 5 views
4

레일 애플리케이션이 하나 있습니다. Ajax 호출을 통해 JSON 데이터를 얻었으므로 JSON 데이터를 애플리케이션 데이터베이스로 가져 오려고합니다. 어떻게 보관할 수 있습니까? 아무도 나를 도울 수 있습니까? 미리 감사드립니다.JSON 데이터를 레일 애플리케이션으로 데이터베이스로 가져올 수 있습니까?

--- 업데이트 ---
내 응용 프로그램에는 작업 모델과 사용자 모델이 있습니다. 사용자에게는 많은 작업이 있고 작업은 사용자에 속합니다. 사용자가 로그인 한 후 Ajax 호출 (jQuery getJSON)을 통해 다른 서비스 공급자로부터 JSON 데이터를 가져온다. 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" 
} 
+0

데이터를 보존 할 방법에 대한 세부 정보를 추가하면 도움이 될 수 있습니다. 데이터베이스 이 데이터가 모델에 해당합니까 아니면 MySQL 데이터베이스에서 원시 JSON 데이터 만 원합니까? –

+0

Hello Pan, 업데이트 정보를 추가했습니다. –

+0

그건 의미가 있습니다. 업데이트 된 정보를 보내 주셔서 감사합니다. getJSON 호출의 샘플 결과를 질문에 추가하면 응답자는 코드 샘플을 작성하여 해당 데이터를 가져올 수 있습니다. MySQL을 사용하고 있습니까? –

답변

19

, 당신은 명령 줄을 통해 설치할 수 있습니다 :

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 
관련 문제