使用 JSONPath 将字符串与多个 JSON 属性值进行比较
我正在 JavaScript 中构建一个搜索建议文本框控件,并尝试找到一种方法来将用户键入的字符串与表示用户联系人列表的 JSON 对象进行比较。
对象如下所示:
var contacts = {
'gmail' : [
{ name : 'Joe Smith', email : '[email protected]' },
{ name : 'James Simpson', email : '[email protected]' }
]
}
使用 JSONPath,我已经能够使用以下命令成功地将用户键入的字符串与联系人对象中的单个字段(即我可以测试姓名或电子邮件)进行比较:
var input = "james";
var search_path = '$.*[?( /' + input + '/i.test(@.name))]';
var results = jsonPath(contacts ,search_path, {resultType:"VALUE"});
JSON {James Simpson}
联系人对象,但如果我输入 Jim 而不是 James,它不会返回任何内容,除非我执行两个单独的 JSONPath 查询 - 一个针对姓名,另一个针对电子邮件。
我正在寻找一种使用 JSONPath 执行 OR 运算符的优雅方法,这样我就可以针对多个 JSON 属性值测试单个字符串。
这是描述我正在寻找的内容的伪代码(无效):
var search_path = '$.*[?( /' + input + '/i.test([ @.name, @.email ]))]';
有人知道如何做到这一点吗?
I'm building a search suggestion text box control in JavaScript and am trying to find a way to compare the string the user typed against a JSON Object that represents the user's contact list.
The JSON Object looks like this:
var contacts = {
'gmail' : [
{ name : 'Joe Smith', email : '[email protected]' },
{ name : 'James Simpson', email : '[email protected]' }
]
}
Using JSONPath, I've been able to successfully compare the string the user typed against a single field in the contact object (ie. I can test the name, or the email) using the following:
var input = "james";
var search_path = '$.*[?( /' + input + '/i.test(@.name))]';
var results = jsonPath(contacts ,search_path, {resultType:"VALUE"});
Which returns the {James Simpson}
contact object, but if I had typed Jim instead of James it would return nothing unless I did two separate JSONPath queries - one against the name and the other against the email.
What I'm looking is an elegant way to do an OR operator with JSONPath so I can test a single string against multiple JSON property values.
Here's the psuedo-code (non-working) that describes what I'm looking for:
var search_path = '$.*[?( /' + input + '/i.test([ @.name, @.email ]))]';
Does anyone know of a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将创建一个更简单的数据结构,将搜索词映射到联系人姓名。获得联系人姓名后,使用
jsonPath
查找整个记录I would create a simpler data structure that maps search terms to a contact name. Once you have a contact name, look up the entire record using
jsonPath
更好的方法是使用 DefiantJS (http://defiantjs.com)。该库使用“search”方法扩展了全局对象 JSON - 您可以使用 XPath 表达式查询 JSON 结构。此方法返回数组中的匹配项(如果未找到匹配项,则为空)。
这是下面代码的有效 JSfiddle;
http://jsfiddle.net/hbi99/z2Erf/
表达式 '//gmail[ contains(., "jim")]' 将查找 GMAIL 下的所有字段,无论字段名称如何。要将搜索显式限制为“姓名”和“电子邮件”字段,则查询应如下所示:
要了解 XPath 的强大功能,请查看此 XPath Evaluator 工具;
http://www.defiantjs.com/#xpath_evaluator
A better way is to use DefiantJS (http://defiantjs.com). This lib extends the global object JSON with the method "search" - with which you can query JSON structure with XPath expressions. This method returns the matches in an array (empty if no matches were found).
Here is a working JSfiddle of the code below;
http://jsfiddle.net/hbi99/z2Erf/
The expression '//gmail[contains(., "jim")]' will find all fields under GMAIL regardless of field name. To explicitly constrict the search to the fields "name" and "email", then the query should look like this:
To get an idea of how powerful XPath is, check out this XPath Evaluator tool;
http://www.defiantjs.com/#xpath_evaluator
如果您使用 Goessner 的解析器,则可以在表达式中使用
||
运算符,如下所示:If you are using Goessner's parser, you can use the
||
operator in your expression as follows: