我有一个小bash程序,调用返回
JSON数据的Web服务.
我自己编写了webservice程序,我完全控制了它的数据源,因此我可以信任返回的数据.
现在我想对数据做些什么.
数据是一个简单,短的键值结构,没有嵌套,如下所示:
{ "asciifile" : "../tmp/data_20120720_105746-01580.txt","excelfile" : "../tmp/data_01580-20120720_105746.xlsx","from" : "Jun 19,2012","msg" : "some info message,for the admin","outfile" : "data--recent.txt","outfile_excel" : "data--recent.txt.xlsx","resolution" : "std" "to" : "Jul 20,"url_comment" : "another info message,for the screen/user","url_outfile" : "http://www.example.com/path/tmp_cached_files/data--recent.txt","url_outfile_excel" : "http://www.example.com/path/tmp_cached_files/data--recent.txt.xlsx",}
现在我使用这个单行程序来反序列化返回到perl代码的json结构.请参阅此代码段的最后一行:
#!/bin/bash cmd=$(curl_or_wget_call_to_webservice) output=$(eval $cmd) outfile_excel=$(echo "$output"| json_xs -f json -t dumper | tee | perl -n0777 -E 'eval "%h=%{$_}"; warn $@ if $@; say $h{outfile_excel}')
例如,我不确定为什么我想出了%{$_}构造.有一个更好的方法吗?是否有更短的方式或更安全的方式来写最后一行?
SE编辑:如果您愿意,您可以将此帖子移至codereview stackexchange网站,但我没有帐户.
编辑:在8个月后重新访问帖子后,我想补充一点,这些天我使用这个内容来获取我的github repos的名称:
wget --quiet --auth-no-challenge --user knbknb --password secret -O - https://api.github.com/user/repos | perl -MJSON -n0777 -E '$r = decode_json($_); map {say $_->{name}} @$r' -