我在Bash变量中捕获了以下命令输出:
{"app_versions":[{"version":"16125","shortversion":"4.4.0","title":"App 1","timestamp":1377277516,"appsize":46110031,"mandatory":false,"minimum_os_version":"6.0","device_family":"iPhone/iPod","id":22,"app_id":25196,"download_url":"https://rink.hockeyapp.net/apps/b5dc72777668ca5716aa6aec8237058f/app_versions/22","status":2,"tags":["alpha"]},{"version":"16126","shortversion":"4.4.0","title":"App 1","timestamp":1377177516,"appsize":46330031,"mandatory":false,"minimum_os_version":"6.0","device_family":"iPhone/iPod","id":20,"app_id":25190,"download_url":"https://rink.hockeyapp.net/apps/b5dc72777668ca5716aa6aec8237058f/app_versions/20","status":2,"tags":["alpha"]}
如何从上面的输出中找到id
的最高值并获取相关的version
?
答案 0 :(得分:1)
您发布的值无效JSON - 它最后缺少几个字符 - 但假设您的实际值 有效,您可以这样写:
perl -e \
' use warnings;
use strict;
use JSON ();
my $highest_id;
my $version;
foreach my $app_version (@{(JSON::from_json $ARGV[0])->{"app_versions"}}) {
my $id = $app_version->{"id"};
if(! defined ($highest_id) || $id > $highest_id) {
$highest_id = $id;
$version = $app_version->{"version"};
}
}
print $version;
' \
"$your_variable"