2017-04-03 67 views
0

Im新增到rails中,並有一個Rails應用程序調用api並將XML數組數據插入到數據庫中。我在訪問XML數據的嵌套部分時遇到問題,它說{分類=} {CategoryDe​​scription = COMEDY}的部分這裏是數組。我不知道如何將分類描述= COMEDY存儲在數據庫中。在數組中檢索嵌套的XML數據

{"Array"=>{"Artist"=>[{"Category"={"CategoryDescription"=>"COMEDY", "ChildCategoryID"=>"xx"}, "Description"=>"Adam Sandler", "ID"=>"14",} 

這是我如何訪問和創建的描述和ID從陣列,但無法弄清楚如何檢索嵌套類別響應。

doc = response.parsed_response 
doc["Array"]["Artist"].each do |category| 

performer = Performer.create( PerformerID: category['ID'], 
         PerformerName: category['Description'], 

我嘗試過在XML響應中訪問和存儲嵌套[類別]。

doc2["Array"]["Artist"]["Category"].each do |category| 

performerCategory = Performer.create(EventID: category['ChildCategoryDescription'],) 

它說它不能將字符串轉換爲整數。任何事情都會有所幫助。

回答

0

如果需要category['ChildCategoryDescription']轉換爲整數,嘗試to_i這樣的:

performerCategory = Performer.create(EventID: category['ChildCategoryDescription'].to_i,)