如何在JSONPath中按字符串过滤? [英] How to filter by string in JSONPath?

查看:613
本文介绍了如何在JSONPath中按字符串过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自Facebook API的JSON响应,如下所示:

I have a JSON response from the Facebook API that looks like this:

{
  "data": [
     {
       "name": "Barack Obama", 
       "category": "Politician", 
       "id": "6815841748"
     }, 
     {
       "name": "Barack Obama's Dead Fly", 
       "category": "Public figure", 
       "id": "92943557739"
     }]
 }

我想对其应用JSONPath,以仅返回类别为政客"的结果.从我阅读的内容来看,我似乎需要做:

I want to apply JSONPath to it to only return results with a category of "Politician". From what I've read, it appears that I need to do:

$.data[?(@.category=='Politician')]

但是根据我发现的测试工具,这是行不通的.我发现了另一个问题,这表明我应该使用"eq"而不是"==,但这也不起作用.我这是怎么了?

but according to the testing tool I found, this doesn't work. I found another question which suggests that I should use "eq" instead of "==", but that doesn't work either. What am I getting wrong here?

推荐答案

您的查询看起来不错,并且您的数据和查询对我来说都可以使用

Your query looks fine, and your data and query work for me using this JsonPath parser. Also see the example queries on that page for more predicate examples.

您使用的测试工具似乎有问题.甚至 JsonPath网站中的示例都返回了错误的结果:

The testing tool that you're using seems faulty. Even the examples from the JsonPath site are returning incorrect results:

例如,给出:

{
    "store":
    {
        "book":
        [ 
            { "category": "reference",
              "author": "Nigel Rees",
              "title": "Sayings of the Century",
              "price": 8.95
            },
            { "category": "fiction",
              "author": "Evelyn Waugh",
              "title": "Sword of Honour",
              "price": 12.99
            },
            { "category": "fiction",
              "author": "Herman Melville",
              "title": "Moby Dick",
              "isbn": "0-553-21311-3",
              "price": 8.99
            },
            { "category": "fiction",
              "author": "J. R. R. Tolkien",
              "title": "The Lord of the Rings",
              "isbn": "0-395-19395-8",
              "price": 22.99
            }
        ],
        "bicycle":
        {
            "color": "red",
            "price": 19.95
        }
    }
}

和表达式:$.store.book[?(@.length-1)].title,该工具将返回所有标题的列表.

And the expression: $.store.book[?(@.length-1)].title, the tool returns a list of all titles.

这篇关于如何在JSONPath中按字符串过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆