嵌套JSON对象的Jsonpath

9

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

我得到了这个:

enter image description here

我试图返回的(输出)是下面这个:

enter image description here

但我无法找到正确的语法来将这两个组合在一行中,并使用一个JSONPATH查询同时返回两个内容。

$.['价值信息'].['平台补偿'].*,这个可能会对你有所帮助。 - Onkar
@Onkar 这将返回嵌套对象的所有字段。这很好。但是我如何获取(例如)“平台父级Dato Id”? - Datacrawler
2个回答

3

Jayway的实现可以让您做到这一点。JSON路径应该是:

$.[*]['Value Information']['Platform Compensation'][*]['Platform mission Id', 'Value Rate']

您可以在http://jsonpath.herokuapp.com/网站上尝试它。


3

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

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

  • Return an array containing all of the Platform Compensation values:

    $.['Value Information'].['Platform Compensation'].['Platform mission Id']
    
  • Return an array containing all of the Platform mission Id values:

    $.['Value Information'].['Platform Compensation']
    
但是您不能使用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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接