486, "url-6ren">
gpt4 book ai didi

ruby-on-rails - 如何访问仅存在于众多属性中的 1 个属性中的哈希值?

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:01 26 4
gpt4 key购买 nike

所以这个有点棘手。

我有一个如下所示的属性:

[22] pry(main)> n.media.meta_info[:response][:outputs]
=> [{"id"=>486,
"url"=>"http://some-video.com/by-fire.mp4",
"label"=>"webmp4",
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848287,
"url"=>"http://some-video.com/by-fire.webm",
"label"=>"webwebm",
"state"=>"finished",
"format"=>"webm",
"type"=>"standard",
"frame_rate"=>30.06,
{"id"=>488848288,
"url"=>"http://some-video.com/by-fire.ogv",
"label"=>"webogv",
"state"=>"finished",
"format"=>"ogg",
"type"=>"standard",
"frame_rate"=>30.059,
{"id"=>488848289,
"url"=>
"https://zencoder-temp-storage-us-east-1.s3.amazonaws.com/",
"label"=>nil,
"state"=>"finished",
"format"=>"mpeg4",
"type"=>"standard",
"frame_rate"=>30.06,
"thumbnails"=>
[{"label"=>nil,
"images"=>
[{"dimensions"=>"56x100",
"file_size_bytes"=>15142,
"format"=>"PNG",
"url"=>"https://some-video.s3.amazonaws.com/uploads/video/video_file/id/by-fire.png"}]}],
"md5_checksum"=>nil}]

我正在尝试访问 thumbnails 信息,特别是缩略图的 URL。

不过我不知道怎么去那里。

当我尝试访问 thumbnails 的嵌套哈希键时,它不起作用:

[23] pry(main)> n.media.meta_info[:response][:outputs][:thumbnails]
TypeError: no implicit conversion of Symbol into Integer
from (pry):22:in `[]'

想法?

最佳答案

输出开头的[{表示返回的是一个数组。您首先需要在包含 thumbnails 键的数组中找到一个元素:

outputs = n.media.meta_info[:response][:outputs]
output_with_thumbnail = outputs.find { |elem| elem.keys.include?('thumbnails') }

然后继续这样:

output_with_thumbnail['thumbnails']

关于ruby-on-rails - 如何访问仅存在于众多属性中的 1 个属性中的哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31084080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com