[英]How to Parse and find JSON element in Ruby?
I'm trying to return the lowest result number that it's name
is my.name
: 我正在尝试返回
name
为my.name
的最低结果数:
{
"total_results"=>"73",
"results"=>
{
"28"=> {"description"=>"description_here", "name"=> "my.name"},
"25"=> {"description"=>"description_here", "name"=> "other1.name"},
"24"=> {"description"=>"description_here", "name"=> "my.name"},
"21"=> {"description"=>"description_here", "name"=> "other2.name"}
}
}
So in the example above it will return 24
. 因此,在上面的示例中,它将返回
24
。 I'm fairly new to Ruby so I'm struggling to find a good way to do this. 我是Ruby的新手,所以我努力寻找一种很好的方法来做到这一点。
Your example is a hash in Ruby 您的示例是Ruby中的哈希
h = {
"total_results"=>"73",
"results"=>
{
"28"=> {"description"=>"description_here", "name"=> "my.name"},
"25"=> {"description"=>"description_here", "name"=> "other1.name"},
"24"=> {"description"=>"description_here", "name"=> "my.name"},
"21"=> {"description"=>"description_here", "name"=> "other2.name"}
}
}
You can use select
to get all matching values, then get min
key of them like 您可以使用
select
获取所有匹配的值,然后获取它们的min
h["results"].select{|k,v| v["name"] == "my.name"}.keys.map(&:to_i).min
# => 24
This is a solution. 这是一个解决方案。 Definitely not the best, but I can at least show you how you can traverse through that hash..
绝对不是最好的,但是我至少可以向您展示如何遍历该哈希。
foo = {
"total_results"=>"73",
"results"=>
{
"28"=> {"description"=>"description_here", "name"=> "my.name"},
"25"=> {"description"=>"description_here", "name"=> "other1.name"},
"24"=> {"description"=>"description_here", "name"=> "my.name"},
"21"=> {"description"=>"description_here", "name"=> "other2.name"}
}
}
bar = nil
foo['results'].each do |k, v|
if v['name'] == "my.name"
if bar.nil? || k < bar
bar = k
end
end
end
puts bar #=> "24"
Try this: 尝试这个:
h = {
"total_results"=>"73",
"results"=>
{
"28"=> {"description"=>"description_here", "name"=> "my.name"},
"25"=> {"description"=>"description_here", "name"=> "other1.name"},
"24"=> {"description"=>"description_here", "name"=> "my.name"},
"21"=> {"description"=>"description_here", "name"=> "other2.name"}
}
}
To get the minimum key: 要获取最小密钥:
min_key = h["results"].keys.min
To get the min_key value 获取min_key值
h["results"][min_key]
Given your posted hash stored in h , you can do the following: 给定您发布的哈希存储在h中 ,您可以执行以下操作:
h['results'].select { |k,v| v['name'] == 'my.name' }.keys.min_by(&:to_i)
#=> "24"
While this one-liner trades compactness for clarity, it's doing the following: 尽管此一线交易为简洁起见以紧凑性为代价,但它正在执行以下操作:
Returning an interim hash containing hashes where the value assigned to the 'name' key contains your desired string value. 返回一个包含哈希的临时哈希,其中分配给“名称”键的值包含所需的字符串值。 For example:
例如:
{"28"=>{"description"=>"description_here", "name"=>"my.name"}, "24"=>{"description"=>"description_here", "name"=>"my.name"}}
Using Enumerable#min_by to coerce the keys to integers for the comparison, then find the smallest value and return it in the original format (eg as a string). 使用Enumerable#min_by将键强制转换为整数以进行比较,然后找到最小值并以原始格式(例如,作为字符串)返回。
It only looks like magic. 它看起来像魔术。 It's just Ruby.
只是Ruby。 :)
:)
h = {
"total_results"=>"73",
"results"=>
{
"28"=> {"description"=>"description_here", "name"=> "my.name"},
"25"=> {"description"=>"description_here", "name"=> "other1.name"},
"24"=> {"description"=>"description_here", "name"=> "my.name"},
"21"=> {"description"=>"description_here", "name"=> "other2.name"}
}
}
h["results"].select{|key,hash| [key, hash] if hash["name"]=="my.name"}.keys.map(&:to_i).min
=> 24
只需一行代码即可找到min_key
:
min_key = your_hash['results'].sort.to_h.invert['my.name']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.