如何在JsonPath中过滤非数组 [英] How to filter a non-array in JsonPath

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

问题描述

使用以下JSON(来自 http://jsonpath.com ):

Using the following JSON (from http://jsonpath.com):

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

仅当firstName是John时,我才想获取根对象.

I would like to get the root object only if firstName is John.

我尝试了这些输入以及许多其他类似的输入:

I have tried these inputs and many other similar ones:

  • $.[?($.firstName == 'John')]
  • $.[?($.'firstName' == 'John')]
  • $.[?(@.firstName == 'John')]
  • $[?($.firstName == "John")]
  • $.[?($.firstName == 'John')]
  • $.[?($.'firstName' == 'John')]
  • $.[?(@.firstName == 'John')]
  • $[?($.firstName == "John")]

似乎过滤仅用于数组,因此这是不受支持的功能.有人知道在Json.NET中执行此操作的方法,还是确认这是不可能的,甚至可能将我指向支持上述功能的库?

It seems as though filtering is only intended for arrays so this is an unsupported function. Does someone know a way to do this in Json.NET, or confirm that it's not possible and maybe point me to a library which supports the above?

我正在使用F#,但这并不重要,因为F#与C#、. NET和NuGet软件包兼容.

I'm using F# but that's not important because F# is compatible with C#, .NET and NuGet packages.

推荐答案

JSON路径旨在在JSON对象中定位数据,而不是对该数据执行某些处理或测试.过滤器表示法用于标识数组中的一项,目的是返回该数据或其一部分.在数组中拥有对象意味着可能有许多具有相同名称的属性必须通过某种其他方式进行过滤才能选择它们的子集.
在对象属性上使用过滤器表示法不是同一回事.具有特定名称的对象中只能有一个属性,因此声明该名称足以唯一地标识它.您可以通过获取$ .firstName然后分别测试值"John"来轻松实现所需的效果

JSON path is intended to locate data in a JSON object and not to perform some processing or testing on that data. The filter notation is used to identify an item in an array with the purpose of returning that data or some part of it. Having objects in an array means that there may be many properties with the same name that have to be filtered by some other means in order to select a subset of them.
Using filter notation on an object property is not the same thing. There can only be one property in an object with a particular name so stating that name is sufficient to identify it uniquely. You can easily achieve the effect you require by getting $.firstName and then testing separately for the value "John"

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

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