【问题标题】:Jsonpath for nested JSON objects嵌套 JSON 对象的 Jsonpath
【发布时间】:2017-12-04 12:45:43
【问题描述】:

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

我得到了这个:

我要返回(输出)的内容如下:

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

【问题讨论】:

  • $.['Value Information'].['Platform Compensation'].* , this 可以帮到你
  • @Onkar 返回嵌套对象的所有字段。这很好。但是我怎样才能获得(例如)“Platform Parent Dato Id”?

标签: json jsonpath


【解决方案1】:

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

你可以在本站试一试http://jsonpath.herokuapp.com/

【讨论】:

    【解决方案2】:

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

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

    • 返回一个包含所有Platform Compensation 值的数组:

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

      $.['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);
    

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 1970-01-01
      • 2019-12-07
      • 2015-08-23
      • 2016-05-22
      • 1970-01-01
      • 2014-02-11
      • 2013-03-18
      • 2019-03-15
      相关资源
      最近更新 更多