con*_*t01 0 ruby xml hash loops
我正在处理像这样的xml:
<fare_master_pricer_reply>
<flight_index>
<group_of_flights>
<flight_details>
</flight_details>
.
.
<flight_details>
</flight_details>
</group_of_flights>
<group_of_flights>
<flight_details>
</flight_details>
.
.
<flight_details>
</flight_details>
</group_of_flights>
.
.
<group_of_flights>
<flight_details>
</flight_details>
.
.
<flight_details>
</flight_details>
</group_of_flights>
</flight_index>
</fare_master_pricer_reply>
Run Code Online (Sandbox Code Playgroud)
这是在哈希对象中给出的.我需要遍历那个哈希,到目前为止我已经编码了这个:
@flights = response.to_hash[:fare_master_pricer_calendar_reply][:flight_index]
while (@flight_groups = @flights[:group_of_flights]) != nil
while (@flight = @flight_groups[:flight_details])
@time_data = @flight[:flight_information][:product_date_time]
@html = "<tr>"
@html += "<td>" + @time_data[:date_of_departure] + "</td>"
@html += "<td>" + @time_data[:date_of_arrival] + "</td>"
@html += "<td>" + @flight[:location][:location_id] + "</td>"
@html += "</tr>"
end
@html = "<tr><td>**</td><td>**</td><td>**</td><td>**</td><td>**</td><td>**</td><td>**</td></td>"
end
Run Code Online (Sandbox Code Playgroud)
但我明白了
TypeError(符号作为数组索引):
在这一行:
while (@flight = @flight_groups[:flight_details])
Run Code Online (Sandbox Code Playgroud)
为什么我的哈希变成一个数组?这是迭代我原始哈希的正确方法吗?
谢谢!!!
bas*_*man 10
迭代哈希的正确方法就是这样
@flights.each do |key, value|
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |