[英]Ruby Array: TypeError (no implicit conversion of Symbol into Integer)
[英]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
這個想法是返回一個包含所有人員及其 3 個標簽的數組。
我知道 JSON 解析數據正在運行,因為注釋掉的代碼運行良好,我可以更改x
分配並運行。 for 循環也有效,因為當我添加計數器時,它會在json['_embedded']['attendeeParticipationResponses']
計算 15 個響應。 所以我知道那里有 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 `[]'
沒關系我修好了! 這是一個語法問題。
我來自更多的 Java 背景,並且 ruby 的循環語法使x
成為一個對象。 我認為它會包含數組索引。 我通過更改循環內的引用來修復它:
first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']
到:
first_name = x['firstName']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.