有没有办法只用 JsonPath 选择所有子数组

问题描述 投票:0回答:2

我有一个如下的json

{
    "Shop": [
      {
        "id": "1",
        "Items": [
          {
            "Item": "Item1"
          }
        ]
      },
      {
        "id": "2",
        "Items": [
          {
            "Item": "Item2"
          }
        ]
      },
      {
        "id": "3",
        "Items": [
          {
            "Item": "Item3"
          }
        ]
      }
    ]
        
}

我想仅使用 JsonPath 选择所有

Items
。我尝试了以下组合,但没有得到任何值

$.[Shop[0], Shop[1], Shop[2]].Items

$.[Shop[0].Items, Shop[1].Items, Shop[2].Items]

提前谢谢您

json jsonpath
2个回答
2
投票

如果我理解正确,您正在寻找

*
通配符来选择数组中的所有元素

$.Shop[*].Items

给我:

[[ { “项目”:“项目1” } ], [ { “项目”:“项目2” } ], [ { “项目”:“项目3” } ] ]


0
投票

使用jsonpath-cli

$.Shop[*].Items[*]

给予

[{"Item":"Item1"},{"Item":"Item2"},{"Item":"Item3"}]

或者,

$.Shop[*].Items.['Item']

给予

["Item1","Item2","Item3"]
© www.soinside.com 2019 - 2024. All rights reserved.