使用 JSONPath 将字符串与多个 JSON 属性值进行比较

发布于 2024-11-29 00:01:30 字数 1160 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜无邪 2024-12-06 00:01:30

我将创建一个更简单的数据结构,将搜索词映射到联系人姓名。获得联系人姓名后,使用 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

天赋异禀 2024-12-06 00:01:30

更好的方法是使用 DefiantJS (http://defiantjs.com)。该库使用“search”方法扩展了全局对象 JSON - 您可以使用 XPath 表达式查询 JSON 结构。此方法返回数组中的匹配项(如果未找到匹配项,则为空)。

这是下面代码的有效 JSfiddle;
http://jsfiddle.net/hbi99/z2Erf/

var data = {
       "gmail": [
          {
             "name": "Joe Smith",
             "email": "[email protected]"
          },
          {
             "name": "James Simpson",
             "email": "[email protected]"
          }
       ]
    },
    res = JSON.search( data, '//gmail[contains(., "jim")]' );

console.log( res[0].name );
// James Simpson

表达式 '//gmail[ contains(., "jim")]' 将查找 GMAIL 下的所有字段,无论字段名称如何。要将搜索显式限制为“姓名”和“电子邮件”字段,则查询应如下所示:

'//gmail[contains(name, "jim") or contains(email, "jim")]'

要了解 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/

var data = {
       "gmail": [
          {
             "name": "Joe Smith",
             "email": "[email protected]"
          },
          {
             "name": "James Simpson",
             "email": "[email protected]"
          }
       ]
    },
    res = JSON.search( data, '//gmail[contains(., "jim")]' );

console.log( res[0].name );
// James Simpson

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:

'//gmail[contains(name, "jim") or contains(email, "jim")]'

To get an idea of how powerful XPath is, check out this XPath Evaluator tool;
http://www.defiantjs.com/#xpath_evaluator

甜尕妞 2024-12-06 00:01:30

如果您使用 Goessner 的解析器,则可以在表达式中使用 || 运算符,如下所示:

var search_path = '$.*[?(/jim/i.test(@.name) || /jim/i.test(@.email))]';

If you are using Goessner's parser, you can use the || operator in your expression as follows:

var search_path = '$.*[?(/jim/i.test(@.name) || /jim/i.test(@.email))]';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文