繁体   English   中英

如何在 JMeter 中使用 JSON 提取器提取 JSON 响应中的嵌套元素

[英]How to extract nested elements in JSON response using JSON extractor in JMeter

我想根据嵌套 JSON 响应中的条件提取一个元素。 使用下面广泛搜索的示例,我想提取“Foo Bar”创作的所有书籍的 storeid:

{
    "store": {
        "storeId": 1
        "book": [
            {
                "category": "reference",
                "author": [
                      {
                       "Nigel Rees",
                       "Foo Bar"
                      }
                ],
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": [
                      { 
                       "Evelyn Waugh"
                      }
                ],
                "title": "Sword of Honour",
                "price": 12.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

我尝试使用如下表达式: $.store[?(@..author=='Foo Bar')].storeId

我还尝试使用以下方法查看是否有任何商店返回: $.store[*].book[?(@.author=='Foo Bar')]

但没有返回结果......请帮助。 谢谢!

您生成的不是有效的 JSON ,因此您将无法在其上使用 JSON Extractor。

如果你修改这个你甚至不能正确复制和粘贴以包含你的storeId的好例子:

{
  "store": {
    "storeId": 1,
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}

您将能够使用以下表达式提取其中一位作者是Nigel ReesstoreId

$.store[?(@.book[?(@.author=="Nigel Rees")])].storeId

演示:

在此处输入图像描述

更多信息:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM