字符串不会转换为JSON

时间:2015-09-08 17:16:23

标签: ruby-on-rails arrays json parsing

我有一个通过变量传递的字符串。我试图将其转换为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 个答案:

答案 0 :(得分:0)

@body.to_json

正如维克多所说,这不是一个json,它是ruby hash