获取JSON中的密钥 [英] Get keys in JSON

查看:359
本文介绍了获取JSON中的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部系统获得以下JSON结果:

I get the following JSON result from an external system:

{
  "key1": "val1",
  "key2": "val2",
  "key3": "val3"
}

现在,我想使用JSONPath显示所有键和所有值.所以我正在寻找某种东西来获取key1,key2和key3作为结果.另外,我想使用一个属性的索引,例如. G. $....[2].key获取"key3"等. 有办法做这样的事吗?

Now I want to display all keys and all values by using JSONPath. So I am looking for something to get key1, key2 and key3 as a result. Additionally I would like to use the index of a property, e. g. $....[2].key to get "key3" etc. Is there a way to do something like this?

推荐答案

我发现tilda ~符号能够检索其所调用的值的键.因此,对于您的示例,这样的查询:

I found that the tilda ~ symbol is able to retrieve the keys of the values it's called upon. So for your example a query like this:

$.*~

返回此:

[
  "key1",
  "key2",
  "key3"
]

另一个示例,如果我们有一个像这样的JSON文档:

Another example, if we had a JSON document like this:

  {
  "key1": "val1",
  "key2": "val2",
  "key3": {
      "key31":"val31",
      "key32":"val32"
  }
}

这样的查询:

$.key3.*~

将返回此:

[
  "key31",
  "key32"
]

请务必注意,这些示例可在 JSONPath.com 和其他一些模拟器/在线工具上使用,但在某些情况下他们没有.可能是由于我在 JSONPath plus 文档中找到了关于tilda(〜)运算符的事实.而不是官方的.

It's important to note that these examples work on JSONPath.com and some other simulators/online tools, but on some they don't. It might come from the fact that I found out about the tilda(~) operator in the JSONPath plus documentation and not the official one.

这篇关于获取JSON中的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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