我有一个来自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;
}
答案 0 :(得分:1)
我需要一个更完整的例子。我得到的测试应用程序和结果如下:
ruleset a8x167 {
meta {
name "Pick - Array of Hashes"
description <<
Testing
>>
author "Sam Curren"
logging on
}
dispatch {}
global {
raw = {
'records' :{
'record' :[
{'availability' :{'$t' :'available'}},
{'availability' :{'$t' :'available'}}
]
}
};
}
rule test {
select when pageview ".*" setting ()
pre {
raw_records = raw.pick("$..record");
availability = raw.pick("$..availability.$t");
}
notify("Hello World", "This is a sample rule.");
}
}
结果:
var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];