【问题标题】:How to get json path for the given requirement?如何获取给定要求的 json 路径?
【发布时间】:2020-08-13 04:45:37
【问题描述】:

我需要找到给定 json 结构的 json 路径

{
  "name": "ninja",
  "contry": "India",
  "Account": [
    {
      "id": "123",
      "orgId": 223,
      "investment": [
        {
          "invetmentId": "111",
          "name": "India tech",
          "performance": [
            {
              "id": "123",
              "performanceSet": [
                {
                  "amount": "210",
                  "currency": "YEN"
                },
                {
                  "amount": "231",
                  "currency": "USD"
                },
                {
                  "amount": "233",
                  "currency": "USD"
                },
                {
                  "amount": "250",
                  "currency": "IND"
                }
              ],
              "loyal": "250"
            }
          ]
        }
      ]
    }
  ]
}

这里我需要从performanceSet 中获取货币为美元的金额,并且只会返回第一次出现该值的金额?

它应该返回

[
   "231"
]

【问题讨论】:

  • 我认为用 json-path 是不可能的。让我们看看有什么办法。

标签: jsonpath json-path-expression


【解决方案1】:

我认为使用 jsonpath 是不可能的。

你可以使用下面的jsonpath。

$.Account[0].investment[0].performance[0].performanceSet[?(@.currency=='USD')]

这会给你

[
  {
    "amount": "231",
    "currency": "USD"
  },
  {
    "amount": "233",
    "currency": "USD"
  }
]

您可以在此处以编程方式选择第一个元素并进一步阅读amount

【讨论】:

    【解决方案2】:

    您可以尝试使用 js2xml 将 json 转换为 XML,然后再使用 xpath。 XPath 更强大

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 2010-10-23
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多