如何在JSONPath中基于多个候选名称选择JToken? [英] How to select JToken based on multiple name candidates in JSONPath?

查看:176
本文介绍了如何在JSONPath中基于多个候选名称选择JToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的JObject:

I have a JObject like this:

{
  name1: {
    value [
    ...
    ]
  }
}

它也可以采用以下形式:

It may also be in the form of:

{
  name2: {
    value [
    ...
    ]
  }
}

因此,我尝试使用单个JSONPath来选择JArray value.有办法做这样的事吗?

So I'm trying to use a single JSONPath to select the JArray value out. Is there a way to do something like this?

$['name1' or 'name2']['value']

推荐答案

基于以下两个SO问题的答案:阵列上的JsonPath AND运算符 ,似乎JSONPath尚未完全支持ANDOR运算符.

Based on the answers to these two SO questions: OR operator in JSONPath? and JsonPath AND Operator on Array, it seems AND and OR operators are not fully supported in JSONPath yet.

但是,我们可以使用并运算符来近似所需的值.基于 JSONPath文档

However, we can use the union operator to approximate what we need. Based on the JSONPath documentation,

Union operator in XPath results in a combination of node sets.
JSONPath allows alternate names or array indices as a set.

这似乎是我们所需要的,但仍然是阵列上的JsonPath AND运算符提到联合运算符在不同的实现之间可能会有一些错误或细微的差别.

This seems to be what we need but still, JsonPath AND Operator on Array mentioned the union operator may have some bugs or subtle differences between different implementations.

Similary,我在这里执行了自己的验证: http://jsonpath.herokuapp.com/.

Similary, I've performed my own verification here: http://jsonpath.herokuapp.com/.

我已经尝试过['name1','name2']['value'],除了Nebhale的实现之外,其他所有实现都正确地解析了我所需要的.因此,我想我的原始问题有解决方案,但是它是针对实现的,因此请首先完全测试您自己的代码,以确保此JSONPath和union运算符可以在您的代码库中使用.

I've tried ['name1','name2']['value'] and all but Nebhale's implementation correctly parses what I need. So I guess there is a solution to my original problem but it is a bit implementation-specific so please fully test your own code first to make sure this JSONPath and union operator will work in your code base.

这篇关于如何在JSONPath中基于多个候选名称选择JToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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