【问题标题】:Jsonpath: can not get the needed value using expressionsJsonpath:无法使用表达式获得所需的值
【发布时间】:2021-04-09 07:56:26
【问题描述】:

这是 JSON 示例

{
  "customerId": 0,
  "authorizedCustomerIds": [
    0
  ],
  "creativeVersionId": 0,
  "description": "string",
  "version": "string",
  "duration": 0,
  "frameRate": 0,
  "previewImage": "string",
  "previewVideo": "string",
  "renderScript": "string",
  "renderScriptType": "string",
  "elements": [
    {
      "id": 0,
      "type": "IMAGE",
      "name": "string1",
      "parentId": 0,
      "presetId": 0,
      "properties": [
        {
          "name": "string",
          "value": {},
          "type": "ALPHABETIC",
          "producesVideo": true,
          "renderScriptId": "string"
        }
      ]
    },
    {
      "id": 1,
      "type": "TEXT",
      "name": "string2",
      "parentId": 0,
      "presetId": 0,
      "properties": [
        {
          "name": "string",
          "value": {},
          "type": "ALPHABETIC",
          "producesVideo": false,
          "renderScriptId": "string"
        }
      ]
    }
  ],
  "globalElements": [
    {
      "id": 0,
      "disabled": true
    }
  ],
  "shots": [
    {
      "name": "string",
      "displayOrder": 0,
      "startFrame": 0,
      "duration": 0,
      "thumbnailFrame": 0,
      "elements": [
        {
          "id": 0,
          "disabled": true
        }
      ]
    }
  ],
  "sizes": [
    {
      "name": "string",
      "displayOrder": 0,
      "main": true,
      "width": 0,
      "height": 0,
      "properties": [
        {
          "templateElementId": 0,
          "propertyName": "string",
          "linked": true,
          "value": {}
        }
      ]
    }
  ]
}

我需要获取类型为 TEXT 且属性“producesVideo”为 false 的元素名称。

我试过这种方法,但它不起作用

$..elements[?(@.type == 'TEXT' && @.properties[?(@.producesVideo == false)])].name

那我需要放心提取路径

.extract().path(); 

使用findfindAll 条件

【问题讨论】:

  • 你使用的是什么实现?这可能是对嵌套查询的支持问题。请注意,目前正在制定规范,因此目前还没有标准化。

标签: json rest-assured jsonpath rest-assured-jsonpath


【解决方案1】:

您使用的是什么 JSONPath 实现?对于该文档和 JSONPath 表达式,一些实现,例如jayway Javajsoncons C++,会返回值结果

[
   "string2"
]

或路径结果

[
   "$['elements'][1]['name']"
]

其他实现,例如Goessner Javascript,无法使用子过滤器消化过滤器表达式。 Christoph Burgmer 的 JSONPath Comparison 涵盖了该案例 here

【讨论】:

    【解决方案2】:

    使用放心的findfindAll 表达式,jsonpath 应该是这样的:

    .extract().path("elements.findAll{it.type == 'TEXT' && it.properties.find{it.producesVideo == false}}.name"); 
    

    【讨论】:

      猜你喜欢
      • 2021-07-25
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2023-03-26
      • 2023-02-17
      • 1970-01-01
      相关资源
      最近更新 更多