我尝试返回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"}
}
}
的最低结果数字:
24
因此,在上面的示例中,它将返回 Process: com.example.nativeaudio, PID: 5895
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.nativeaudio-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.nativeaudio-2/lib/x86, /vendor/lib, /system/lib]]] couldn't find "libnative-audio-jni.so"
at java.lang.Runtime.loadLibrary(Runtime.java:367)
at java.lang.System.loadLibrary(System.java:1076)
at com.example.nativeaudio.NativeAudio.<clinit>(NativeAudio.java:313)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
。我对Ruby很新,所以我很难找到一个很好的方法来做到这一点。
答案 0 :(得分:2)
你的例子是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"}
}
}
您可以使用select
获取所有匹配的值,然后获取它们的min
键,如
h["results"].select{|k,v| v["name"] == "my.name"}.keys.map(&:to_i).min
# => 24
答案 1 :(得分:0)
这是一个解决方案。绝对不是最好的,但我至少可以告诉你如何遍历那个哈希..
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"
答案 2 :(得分:0)
试试这个:
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"}
}
}
获取最小密钥:
min_key = h["results"].keys.min
获取 min_key 值
h["results"][min_key]
答案 3 :(得分:0)
鉴于您在 h 中存储了已发布的哈希值,您可以执行以下操作:
h['results'].select { |k,v| v['name'] == 'my.name' }.keys.min_by(&:to_i)
#=> "24"
虽然为了清晰起见,这个单行代表交换紧凑性,但它正在做以下事情:
返回包含哈希值的临时哈希值,其中分配给“name”键的值包含所需的字符串值。例如:
{"28"=>{"description"=>"description_here", "name"=>"my.name"},
"24"=>{"description"=>"description_here", "name"=>"my.name"}}
使用Enumerable#min_by强制键进行比较,然后找到最小值并以原始格式返回(例如作为字符串)。
只有看起来就像魔法一样。这只是Ruby。 :)
答案 4 :(得分:0)
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
答案 5 :(得分:0)
仅通过一行代码查找min_key
:
min_key = your_hash['results'].sort.to_h.invert['my.name']