Jmeter - 如何验证响应部分json

时间:2018-05-18 07:35:07

标签: json jmeter

我有以下json:

{
"content":
[
    {
        "id":1,
        "userId":2,
        "storeId":8,
        "userFirstName":"Max",
        "userLastName":"Mustermann",
       "city":"Berlin",
        "spendQuantity":100,

        "paymentStatus":"UNPAID",
        "userBalanceStatus":null,
        "rateObject":
        {
            "identifier":23,
            "id":"432",
            "rate":"1.9345345",
            "symbol":"USD",
            "rank":2,

        }
    },
    {
        "id":2,
        "userId":2,
        "storeId":3,
        "userFirstName":"Newman",
        "userLastName":"Mustermann",
       "city":"Berlin",
        "spendQuantity":1000,

        "paymentStatus":"UNPAID",
        "userBalanceStatus":null,
        "rateObject":
        {
            "identifier":3,
            "id":"234",
              "rate":"1.922222245345",
            "symbol":"USD",
            "rank":2,

        }
    },
    {
        "id":3,
        "userId":2,
        "storeId":3,
        "userFirstName":"Newman",
        "userLastName":"Mustermann",
       "city":"Munich",
        "spendQuantity":3000,

        "paymentStatus":"UNPAID",
        "userBalanceStatus":null,
        "rateObject":
        {
            "identifier":2332,
            "id":"234",
               "rate":"3.234234",
            "symbol":"USD",
            "rank":2,

        }
    },
    {
        "id":4,
        "userId":2,
        "storeId":3,
        "userFirstName":"Newman",
        "userLastName":"Mustermann",
       "city":"Essen",
        "spendQuantity":4000,

        "paymentStatus":"UNPAID",
        "userBalanceStatus":null,
        "rateObject":
        {
            "identifier":234,
            "id":"234",
               "rate":"333.234234",
            "symbol":"USD",
            "rank":2,

        }
    }

}

但我需要部分验证它 - 只有嵌套的jsons中的城市是柏林或埃森的字段,但没有rateObject(我不需要验证这部分)。换句话说,我需要使用ids-1,2,4验证嵌套的jsons - 所有字段,而没有rateObject中的信息。

1 个答案:

答案 0 :(得分:0)

部分答案和建议:

  1. 我们可以在JSON查询中应用过滤条件来获取匹配的详细信息。
  2. 例如:要获取上述城市的ID,

    JSON查询:

    $.content[?(@.city=="Berlin" || @.city=="Essen")].id
    

    <强>输出:

    [
        1,
        2,
        4
    ]
    

    同样,您可以使用过滤器JSON查询断言所有必需字段。 JMeter JSON Extractor一次只提供一个值。因此,您可以添加一些逻辑来验证所有事件,也可以通过指定id索引来添加多个验证($ .content [?(@。city ==“Berlin” “|| @ .city ==”Essen“)]。id [0] - &gt;它给出了第一次出现的Id)

    1. 如果您想验证多个字段,那么您可以在Bean Shell Post Processor中编写自定义脚本。(请参阅以下链接,您将会有所了解)
    2. Extracting JSON Response using Bean Shell Postprocessor

      如果您使用的是Bean Shell Post Processor,那么所需的Java jar文件应该放在JMeter ClassPath中(文件夹:apache-jmeter-4.0 \ lib \ ext)