无法编写jsonpath查询以返回已过滤数组的第一个对象

时间:2015-01-22 17:51:01

标签: jsonpath

使用ashphy json path evaluator和以下JSON:

{ "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
    }
  }
}

我想写一个过滤类别的查询,例如"小说",然后只返回第一个元素。我似乎无法弄明白。

例如,$..book[?(@.category == "fiction")]将返回3本书的数组,但我只想要第一本书:

{ "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      }

可以吗?显而易见的事情似乎是添加一个" [0]"到最后,但它不起作用。

1 个答案:

答案 0 :(得分:0)

我检查了分析网站(注意:这是来自测试http://ashphy.com/JSONPathOnlineEvaluator/):

enter image description here

放置[0]索引可以正常工作。该站点仅接受jsonPath(jsonObj,查询字符串)参数内部的内容,因此索引引用必须超出此站点不执行的评估范围。

jsonPath(foo, '$..book[?(@.category == "fiction")]')[0]