嵌套 JSON 对象的 Jsonpath

Apo*_*mer 7 json jsonpath

我有一个带有嵌套字段的 JSON:

  [
    {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111112",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111112",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "14.0000",
          "Value": 109.0909
        }
      ]
    }
  },
  {
    "Platform Parent Dato Id": "23768",
    "Platform Dato Id": "24138",
    "Platform Dato Name": "Random Europe",
    "Platform mission Id": "111113",
    "Platform submission Id": "638687",
    "Platform submission Flight Id": "863524",
    "Start Date": "2017-12-01",
    "End Date": "2017-12-02",
    "Platform Compensation": 109.0909,
    "Total Value": 909.0909,
    "Goal": "200000.0000",
    "Value Information": {
      "Platform Compensation": [
        {
          "Platform mission Id": "111113",
          "Platform submission Id": "638687",
          "Platform submission Flight Id": "863524",
          "Value Rate": "12.0000",
          "Value": 109.0909
        }
      ]
    }
  }
  ]
Run Code Online (Sandbox Code Playgroud)

我正在使用 JSONPATH 以便Value RateValue Information巢中获取。

我已经在这个网站上粘贴了我的 JSON 文本:http : //jsonpath.com/ 并在使用这一行之后:

$[*].['Platform Compensation'].['Value Rate']
Run Code Online (Sandbox Code Playgroud)

我得到这个:

在此处输入图片说明

并在使用此行后:

$.['Value Information'].['Platform Compensation'].['Platform mission Id']
Run Code Online (Sandbox Code Playgroud)

我得到这个:

在此处输入图片说明

我试图返回(输出)如下:

在此处输入图片说明

但我找不到正确的语法将这两者组合在一行中,并用一个 JSONPATH 查询返回两者。

gly*_*ing 4

jsonpath可用于为给定表达式选择,并且在某些实现中为自定义谓词选择值,但它不支持投影。

您可以使用jsonpath它来过滤给定的 JSON。例如:

但您不能用于读取键和值jsonpath的子集。要读取键和值的子集,您需要使用 JSON 反/序列化库。Java 世界中常用的库有JacksonGson

这是使用 Jackson 的示例:

String json = "...";

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> asMap = mapper.readValue(json, Map.class);

Map<String, Object> transformed = new HashMap<>();
transformed.put("Platform mission Id", asMap.get("Platform mission Id"));
transformed.put("Value Rate", asMap.get("Value Rate"));

String result = mapper.writeValueAsString(transformed);
Run Code Online (Sandbox Code Playgroud)