2015-09-08 38 views
-1

我有一個字符串傳遞給變量。我試圖將其轉換爲JSON,以便我可以將其分解,但我一直收到JSON解析器錯誤。字符串不會轉換爲JSON

這是傳給我的字符串(最初是從一種API,所以應該正確格式化):

[{"FlightSegment"=>[{"DepartureAirport"=>{"LocationCode"=>"LAX"}, "ArrivalAirport"=>{"LocationCode"=>"FLL"}, "MarketingAirline"=>{"Code"=>"NK"}, "ArrivalTimeZone"=>{"GMTOffset"=>-4}, "TPA_Extensions"=>{"eTicket"=>{"Ind"=>true}}, "StopQuantity"=>0, "ElapsedTime"=>286, "ResBookDesigCode"=>"U", "MarriageGrp"=>"O", "Equipment"=>{"AirEquipType"=>319}, "DepartureDateTime"=>"2015-09-18T21:00:00", "ArrivalDateTime"=>"2015-09-19T04:46:00", "FlightNumber"=>310, "OnTimePerformance"=>{"Level"=>7}, "OperatingAirline"=>... it goes on for a while, but repeating itself. 

和代碼我試圖使用方法:

<% parsed_list = JSON.parse(@body) %> 
<%= parsed_list %> 

Unfortunatley我不斷收到意想不到的令牌:

399: unexpected token at '{"FlightSegment"=>[{"DepartureAirport"=>{"LocationCode"=>"LAX"}, "ArrivalAirport"=>{"LocationCode"=>"FLL"}, "MarketingAirline"=>{"Code"=>"NK"}, "ArrivalTimeZone"=>{"GMTOffset"=>-4}, "TPA_Extensions"=>{"eTicket"=>{"Ind"=>true}}, "StopQuantity"=>0, "ElapsedTime"=>286, "ResBookDesigCode"=>"U", "MarriageGrp"=>"O", "Equipment"=>{"AirEquipType"=>319}, "DepartureDateTime"=>"2015-09-18T21:00:00", "ArrivalDateTime"=>"2015-09-19T04:46:00", "FlightNumber"=>310, "OnTimePerformance"=>{"Level"=>7}, "OperatingAirline"=>{"FlightNumber"=>310, "Code"=>"NK"}, "DepartureTimeZone"=>{"GMTOffset"=>-7}}], "ElapsedTime"=>286} 

有什麼想法? 謝謝!

+1

該字符串不是JSON,這是一個紅寶石哈希值。 – victorkohl

+0

@victorkohl - 這很奇怪。這些數據來自API的文檔說它是作爲JSON發送的。 – RossRobertMcKee

回答

0
@body.to_json 

維克多說,這不是一個JSON,它的Ruby哈希