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

问题描述

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

{
    "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')]

但未返回任何结果...请帮助。谢谢!

解决方法

您产生了is not a valid JSON,因此您将无法在其上使用JSON Extractor。

如果您修改this good example,甚至无法正确复制并粘贴以包含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
      },"author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99
      },"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
}

您将可以使用以下表达式提取storeId的作者之一Nigel Rees

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

演示:

enter image description here

更多信息:

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...