[英]Picking an array of hashes from a hash
I have a hash coming back from an XML datasource that looks like this: 我有一个从XML数据源返回的哈希,如下所示:
{...,
'records' :{
'record' :[
{'availability' :{'$t' :'available'}, ...},
{'availability' :{'$t' :'available'}, ...}
]
}
};
I'd like to get all the record
hashes into an array so I can filter()
it and do some other operations. 我想把所有record
哈希值都放到一个数组中,这样我就可以filter()
它并做一些其他的操作。 However, when I have this statement in my pre
block, 但是,当我在pre
block中有这个声明的时候,
raw_records = raw.pick("$..record");
the array that gets returned is an array of two empty strings: 返回的数组是一个包含两个空字符串的数组:
var raw_records = ['', ''];
The odd thing is that I can pick out just availability
with expected results: 奇怪的是,我可以选择具有预期结果的availability
:
availability = raw.pick("$..availability.$t");
producing 生产
var availability = ['available', 'available'];
What's wrong with my first pick()
? 我的第一个pick()
什么问题?
EDIT: Here is a more complete version that should help with reproducing the problem. 编辑:这是一个更完整的版本,应该有助于重现问题。 It's slightly different, since I'm using the JSON version of the web service now: 它略有不同,因为我现在正在使用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;
}
I'm going to need a more complete example. 我需要一个更完整的例子。 The test app and results I got are below: 测试应用程序和我得到的结果如下:
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.");
}
}
And Results: 结果:
var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.