如何改进这个perl / bash one-liner来反序列化json数据
发布时间:2020-12-15 23:20:12 所属栏目:大数据 来源:网络整理
导读:我有一个小bash程序,调用返回 JSON数据的Web服务. 我自己编写了webservice程序,我完全控制了它的数据源,因此我可以信任返回的数据. 现在我想对数据做些什么. 数据是一个简单,短的键值结构,没有嵌套,如下所示: { "asciifile" : "../tmp/data_20120720_105746
我有一个小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' - 解决方法
有
jshon.你可以简单地打电话
curl http://somehere.tld/data.json | jshon -e url_outfile_excel 哪个会打印给定键的值. 顺便说说.控制Web服务并不能使输入值得信赖.调用eval时要小心. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |