如何改进这个perlbash one-liner来反序列化json数据

如何改进这个perlbash one-liner来反序列化json数据,第1张

概述我有一个小bash程序,调用返回 JSON数据的Web服务. 我自己编写了webservice程序,我完全控制了它的数据源,因此我可以信任返回的数据. 现在我想对数据做些什么. 数据是一个简单,短的键值结构,没有嵌套,如下所示: { "asciifile" : "../tmp/data_20120720_105746-01580.txt", "excelfile" : "../tmp/d 我有一个小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/bashcmd=$(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时要小心.

总结

以上是内存溢出为你收集整理的如何改进这个perl / bash one-liner来反序列化json数据全部内容,希望文章能够帮你解决如何改进这个perl / bash one-liner来反序列化json数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1260541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存