简体   繁体   English

嵌套数组 ruby​​ TypeError:没有将哈希隐式转换为整数

[英]nested array ruby TypeError: no implicit conversion of Hash into Integer

def get_att(webinar_id)
  finalArray = []
  api_service = ApiService.new
  response = api_service.get_attendees(webinar_id)
  json = JSON.parse(response.body)

  for x in json['_embedded']['attendeeParticipationResponses']
    first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']
    last_name = json['_embedded']['attendeeParticipationResponses'][x]['lastName']
    email = json['_embedded']['attendeeParticipationResponses'][x]['email']
    finalArray << [first_name, last_name, email]
  end

  # x = 2
  # first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']
  # last_name = json['_embedded']['attendeeParticipationResponses'][x]['lastName']
  # email = json['_embedded']['attendeeParticipationResponses'][x]['email']
  # finalArray << [first_name, last_name, email]

  Rails.logger.info(finalArray)
end

The idea is to return an array of arrays with all of the people and their 3 tags.这个想法是返回一个包含所有人员及其 3 个标签的数组。

I know that the JSON parse data is working because the commented out code works perfectly and I can change x assignment and it runs.我知道 JSON 解析数据正在运行,因为注释掉的代码运行良好,我可以更改x分配并运行。 The for loop also works because it counts 15 responses in json['_embedded']['attendeeParticipationResponses'] when I add a counter. for 循环也有效,因为当我添加计数器时,它会在json['_embedded']['attendeeParticipationResponses']计算 15 个响应。 So I know the 15 people are there and I can add them one by one into the final array (which I got to hold multiple arrays by the way), but for some reason the second I put it in the for loop I get the weird error which fully reads:所以我知道那里有 15 个人,我可以将它们一个一个地添加到最终数组中(顺便说一下,我必须保存多个数组),但是出于某种原因,第二次我将它放入 for 循环中,我得到了奇怪的完全读取的错误:

TypeError: no implicit conversion of Hash into Integer from /Users/josh/Documents/GitHub/schools_health/lib/go_to_webinar/to_pipeline.rb:19:in `[]'

Never mind I fixed it!没关系我修好了! It was a syntax issue.这是一个语法问题。

I come from more of a Java background and the loop syntax for ruby makes x an object.我来自更多的 Java 背景,并且 ruby​​ 的循环语法使x成为一个对象。 I thought it would contain the array index.我认为它会包含数组索引。 I fixed it by changing the references inside the loop from:我通过更改循环内的引用来修复它:

first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']

to:到:

first_name = x['firstName']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby Array:TypeError(没有将Symbol隐式转换为Integer) - Ruby Array: TypeError (no implicit conversion of Symbol into Integer) TypeError-在Ruby on Rails数组中没有将String隐式转换为Integer - TypeError - no implicit conversion of String into Integer in array Ruby on Rails Ruby on Rails数组-不将符号隐式转换为整数 - Ruby on rails array - no implicit conversion of symbol into integer Ruby程序返回错误“将字符串隐式转换为Integer(TypeError)” - Ruby program returns error “implicit conversion of String into Integer (TypeError)” Ruby:循环并调用关联数组(TypeError不会将String隐式转换为Integer) - Ruby: Looping and calling associative arrays (TypeError no implicit conversion of String into Integer) Rails-升级到ruby 2.2.2-没有将数组隐式转换为String(TypeError) - Rails- upgrading to ruby 2.2.2 - no implicit conversion of Array into String (TypeError) Ruby类型错误:没有将符号隐式转换为整数 - Ruby Type Error: no implicit conversion of Symbol into Integer 将数组添加到非Rails模型会产生TypeError-不会将String隐式转换为Integer - Adding array to non-rails model gives TypeError - no implicit conversion of String into Integer Ruby,哈希和数组中的嵌套循环 - Nested Loop in Ruby, Hash and Array 红宝石将嵌套数组变成哈希 - ruby turn a nested array into a hash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM