简体   繁体   English

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

[英]how to improve this perl/bash one-liner to deserialize json data

I have a little bash program that calls a webservice that returns JSON data. 我有一个小bash程序,调用返回JSON数据的Web服务。

I have written the webservice program myself, I have complete control over its data sources, and thus I can trust the data that is returned. 我自己编写了webservice程序,我完全控制了它的数据源,因此我可以信任返回的数据。

Now I want to do something with the data. 现在我想对数据做些什么。

The data is a simple, short key-value structure without nesting, and looks like this: 数据是一个简单,短的键值结构,没有嵌套,如下所示:

{
  "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, 2012",
   "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",

}

Now I am using this one-liner to deserialize the json structure returned to perl code. 现在我使用这个单行程序来反序列化返回到perl代码的json结构。 See last line of this snippet: 请参阅此代码段的最后一行:

#!/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}')

For example, I'm not sure why I came up with the %{$_} construct. 例如,我不确定为什么我想出了%{$ _}构造。 Is there a better way to do this? 有一个更好的方法吗? Is there a shorter way or a safer way to write the last line? 是否有更短的方式或更安全的方式来写最后一行?

SE Editors: if you wish, you may move this post to the codereview stackexchange site, but I don't have an account there. SE编辑:如果您愿意,您可以将此帖子移至codereview stackexchange网站,但我没有帐户。

Edit: After revisiting the post after 8 months , I'd like to add that these days I use this one liner for getting the name of my github repos: 编辑:在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' -

Perl can decode JSON itself, so the next should give some idea, using LWP::Simple to get some json data. Perl可以解码JSON本身,所以接下来应该给出一些想法,使用LWP :: Simple来获取一些json数据。

perl -MLWP::Simple -MJSON \
-e '$ref = decode_json(get("http://your.url/to/webservice")); print $ref->{outfile_excel}'

The $ref contains a perl structure of all JSON data, print out as you want it.. $ ref包含所有JSON数据的perl结构,可以根据需要打印出来。

There is jshon . jshon You could simply call something like 你可以简单地称之为

curl http://somehere.tld/data.json | jshon -e url_outfile_excel

Which would print the value for the given key. 哪个会打印给定键的值。

By the way. 顺便说说。 Having control over the webservice doesn't make the input trustworthy. 控制Web服务并不能使输入值得信赖。 Be careful when calling eval . 调用eval时要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM