2016-05-30 50 views
2

a = {「item」=> {「id」=>「34567」,「name」=>「AAX item 1 Inventory23」, 「description」=>「AAX item 1 Inventory23」,「unit_price」=>「2342」, 「item_type」=>「Inventory」,「track_qty_on_hand」=>「true」, 「qty_on_hand」=>「50000」, 「inv_start_date」=>「(3i)」=>「4」,「(2i)」=>「5」, 「(1i)」=>「2016」}},「company_id」=>「1」 「ITEM_TYPE」=> 「清單」, 「量」=> 「1232」}無法使用欄中的列(:)來讀取散列中的數據

輸出:

a ['item'] = {「id」=>「34567」,「name」=>「AAX item 1 Inventory23」, 「description」=>「AAX item 1 Inventory23」,「unit_price」=> 2342「, 」item_type「=>」庫存「,」track_qty_on_hand「=>」true「, 」qty_on_hand「=>」50000「,」inv_start_date「=>」{3i「=>」4「 (2I) 「=>」 5" , 「(1I)」=> 「2016」}}

一個[:項目] =零

所以,如何獲得數據作爲[:項目]。我必須改變什麼?

回答

0

可以使用symbolize_keys改變象徵哈希鍵

a = {"item"=>{"id"=>"34567", "name"=>"AAX item 1 Inventory23", "description"=>"AAX item 1 Inventory23", "unit_price"=>"2342", "item_type"=>"Inventory", "track_qty_on_hand"=>"true", "qty_on_hand"=>"50000", "inv_start_date"=>{"(3i)"=>"4", "(2i)"=>"5", "(1i)"=>"2016"}}, "company_id"=>"1", "item_type"=>"Inventory", "amount"=>"1232"} 

output = a.symbolize_keys 

結果:

{:item=>{"id"=>"34567", "name"=>"AAX item 1 Inventory23", "description"=>"AAX item 1 Inventory23", "unit_price"=>"2342", "item_type"=>"Inventory", "track_qty_on_hand"=>"true", "qty_on_hand"=>"50000", "inv_start_date"=>{"(3i)"=>"4", "(2i)"=>"5", "(1i)"=>"2016"}}, :company_id=>"1", :item_type=>"Inventory", :amount=>"1232"} 
+0

謝謝你給我的答案 –