TypeError:没有将Symbol隐式转换为Integer - Ruby on Rails

时间:2016-01-03 11:33:14

标签: ruby-on-rails

我有一个非常基本的问题。

我尝试了like this one

发布的解决方案

然而它对我不起作用。

我的代码是:

#Logic for connecting to end point and getting the response is above this line...

puts res.body
Rails.logger.debug "Response from endpoint: #{res.body}"

# Once a successful response is received, update the corresponding service table.

content = Hash.new
Rails.logger.debug "Content initial value: #{content}"

content = res.body
Rails.logger.debug "Content loaded value: #{content}"

if content[:result].length != 0
  if content[:result][:hotel][:@type] == "LodgingReservation"

#More logic below this line . . .

回复的格式,即" res.body"是json,它看起来像这样

{"message": "OK", "code": 200, "id": "de97c01a-b206-11e5-8976-0a5d4c2d1469", "result": {"hotel": {"reservationFor": {"telephone": "+123456", "@type": "LodgingBusiness", "x-days": "6"}, "reservationStatus": "http://schema.org/ReservationConfirmed", "broker": {"@type": "Organization", "name": "xyz"}, "checkoutTime": "2014-04-06T12:00:00", "reservationId": "426396323", "checkinTime": "2014-03-31T15:00:00", "@context": "http://schema.org", "@type": "LodgingReservation"}}}

当我尝试运行代码时,它会给出" TypeError:"在这一行 - if content[:result].length != 0

以下是调试器的输出:

Content initial value: {}

Content loaded value: {"message": "OK", "code": 200, "id": "de97c01a-b206-11e5-8976-0a5d4c2d1469", "result": {"hotel": {"reservationFor": {"telephone": "+123456", "@type": "LodgingBusiness", "x-days": "6"}, "reservationStatus": "http://schema.org/ReservationConfirmed", "broker": {"@type": "Organization", "name": "xyz"}, "checkoutTime": "2014-04-06T12:00:00", "reservationId": "426396323", "checkinTime": "2014-03-31T15:00:00", "@context": "http://schema.org", "@type": "LodgingReservation"}}}

正如您从输出"内容"不是哈希。我想弄明白为什么?

当我在 rails控制台 中逐行运行此代码时,它正在运行

>> c = Hash.new
=> {}
>> c = {"message": "OK", "code": 200, "id": "de97c01a-b206-11e5-8976-0a5d4c2d1469", "result": {"hotel": {"reservationFor": {"telephone": "+123456", "@type": "LodgingBusiness", "x-days": "6"}, "reservationStatus": "http://schema.org/ReservationConfirmed", "broker": {"@type": "Organization", "name": "xyz"}, "checkoutTime": "2014-04-06T12:00:00", "reservationId": "426396323", "checkinTime": "2014-03-31T15:00:00", "@context": "http://schema.org", "@type": "LodgingReservation"}}}
=> {:message=>"OK", :code=>200, :id=>"3048f3f4-b205-11e5-849c-0a5d4c2d1469", :result=>{:hotel=>{:reservationFor=>{:telephone=>"+123456", :@type=>"LodgingBusiness", :"x-days"=>"6", :reservationStatus=>"http://schema.org/ReservationConfirmed", :broker=>{:@type=>"Organization", :name=>"xyz"}, :checkoutTime=>"2014-04-06T12:00:00", :reservationId=>"426396323", :checkinTime=>"2014-03-31T15:00:00", :@context=>"http://schema.org", :@type=>"LodgingReservation"}}}
>> c[:result].length
=> 1
>> c[:result].length !=0
=> true
>> c[:result][:hotel][:@type]
=> "LodgingReservation"
>> c[:result][:hotel][:@type] == "LodgingReservation"
=> true

尽可能从rails控制台完美运行。关于如何解决这个奇怪问题的任何想法?!

1 个答案:

答案 0 :(得分:0)

正在将密钥转换为字符串,请尝试

mViewPager.setPagingEnabled(false);