我有一个从 XML 数据源返回的哈希,如下所示:
{...,
'records' :{
'record' :[
{'availability' :{'$t' :'available'}, ...},
{'availability' :{'$t' :'available'}, ...}
]
}
};
我想把所有的record
散列放到一个数组中,这样我就可以filter()
做一些其他的操作了。但是,当我在我的pre
块中有这个声明时,
raw_records = raw.pick("$..record");
返回的数组是两个空字符串的数组:
var raw_records = ['', ''];
奇怪的是,我可以挑选出availability
预期的结果:
availability = raw.pick("$..availability.$t");
生产
var availability = ['available', 'available'];
我的第一个怎么了pick()
?
编辑:这是一个更完整的版本,应该有助于重现问题。略有不同,因为我现在使用的是 JSON 版本的 Web 服务:
global {
datasource hbll <- "https://svc.lib.byu.edu/services/catalog/v1/search/?field=isbn&format=json&terms=";
}
rule new_rule {
select when pageview "amazon.com/.*/?dp/(.*)/" setting (isbn)
pre {
//This is the array with two empty strings...
raw = datasource:hbll(isbn);
myfilter = function(x) { x.pick("availability") eq "available"; };
records = raw.filter(myfilter);
len = records.length();
availability = records.pick("$..availability");
middleman = len > 1 => availability[0] | availability;
available = middleman eq "available" => true | false;
url_list = records.pick("$..url");
url = len > 1 => url_list[0] | url_list;
msg = <<
<p>This book is available for checkout at the BYU Library.</p>
<a href="#{url}">More information</a>
>>;
}
notify("BYU Harold B. Lee Library", msg) with sticky=true;
}