嵌套 JSON 个对象的 Jsonpath

Jsonpath for nested JSON objects

我有一个带有嵌套字段的 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
        }
      ]
    }
  }
  ]

我正在使用 JSONPATH 以便从 Value Information 巢中获取 Value Rate

我已将我的 JSON 文本粘贴到此网站:http://jsonpath.com/ 使用此行后:

$[*].['Platform Compensation'].['Value Rate']

我得到这个:

使用此行后:

$.['Value Information'].['Platform Compensation'].['Platform mission Id']

我得到这个:

我正在尝试 return(输出)如下:

但我找不到正确的语法将这两个组合在一行中,return 都与一个 JSONPATH 查询组合。

jsonpath 可用于给定表达式的 select values 和 - 在某些实现中 - 用于自定义谓词,但它不支持预测。

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

  • Return 包含所有 Platform Compensation 值的数组:

    $.['Value Information'].['Platform Compensation'].['Platform mission Id']
    
  • Return 包含所有 Platform mission Id 值的数组:

    $.['Value Information'].['Platform Compensation']
    

但是您不能使用 jsonpath 来读取 键和值 的子集。要读取键和值的子集,您需要使用 JSON de/serialisation 库。 Java 世界中常用的库是 Jackson and Gson.

这是一个使用 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);

Jayway 实现让你做到这一点。 jsonpath 将是 $.[*]['Value Information']['Platform Compensation'][*]['Platform mission Id', 'Value Rate']

你可以在这个网站上试一下http://jsonpath.herokuapp.com/