【发布时间】:2010-12-17 02:50:43
【问题描述】:
我编写了一个小的 Perl 脚本来从给定键名的 JSON 格式字符串中提取所有值(如下所示)。因此,如果我将 Perl 脚本的命令行开关设置为 id,那么它将从下面的 JSON 示例返回 1,2 和 stringVal。这个脚本可以完成这项工作,但我想看看其他人如何使用其他 unix 风格的工具(如 awk、sed 或 perl 本身)来解决同样的问题。谢谢
{
"id":"1",
"key2":"blah"
},
{
"id":"2",
"key9":"more blah"
},
{
"id":"stringVal",
"anotherKey":"even more blah"
}
提取 JSON 值的 perl 脚本摘录:
my @values;
while(<STDIN>) {
chomp;
s/\s+//g; # Remove spaces
s/"//g; # Remove quotes
push @values, /$opt_s:([\w]+),?/g; # $opt_s is a command line switch for the key to find
}
print join("\n",@values);
【问题讨论】: