如何提取json路径并找到数组长度?

时间:2022-09-28 16:02:53

how to extract JSON path and find array length using java? for my below response data. I need to validate array length should be equal to '7' in Jmeter assertion.

如何使用java提取JSON路径并查找数组长度?我的以下回复数据。我需要在Jmeter断言中验证数组长度应该等于'7'。

[
  [
    "Week",
    "Event Count"
  ],
  [
    "3/13/17",
    "1"
  ],
  [
    "3/20/17",
    "1"
  ],
  [
    "3/27/17",
    "1"
  ],
  [
    "4/3/17",
    "1"
  ],
  [
    "4/10/17",
    "1"
  ],
  [
    "4/17/17",
    "1"
  ]
]

1 个解决方案

#1


2  

  1. Add JSON Extractor as a child of the request which produces the above JSON response and configure it as follows:

    将JSON Extractor添加为请求的子节点,该请求生成上述JSON响应并按如下方式对其进行配置:

    • Variable names: anything meaningful, i.e. week
    • 变量名称:任何有意义的东西,即周

    • JSON Path Expressions: $[*]
    • JSON路径表达式:$ [*]

    • Match No: -1

      比赛号:-1

      如何提取json路径并找到数组长度?

    This will produce the following JMeter Variables (you can validate them using Debug Sampler):

    这将生成以下JMeter变量(您可以使用Debug Sampler验证它们):

    week_1=["Week","Event Count"]
    week_2=["3\/13\/17","1"]
    week_3=["3\/20\/17","1"]
    week_4=["3\/27\/17","1"]
    week_5=["4\/3\/17","1"]
    week_6=["4\/10\/17","1"]
    week_7=["4\/17\/17","1"]
    week_matchNr=7
    

    You are particularly interested in the latter one

    你对后者特别感兴趣

    如何提取json路径并找到数组长度?

  2. Add Response Assertion as a child of the same request and configure it as follows:

    将Response Assertion添加为同一请求的子节点,并按如下方式对其进行配置:

    • Apply to: JMeter Variable -> week_matchNr
    • 适用于:JMeter变量 - > week_matchNr

    • Pattern Matching Rules: Equals
    • 模式匹配规则:等于

    • Patterns to Test: 7
    • 要测试的模式:7

    如何提取json路径并找到数组长度?

    This way your sampler will pass if number of matches will be equal to 7 and otherwise it will fail. See How to Use JMeter Assertions in Three Easy Steps article to learn more about using assertions in JMeter tests.

    这样,如果匹配数等于7,则采样器将通过,否则将失败。有关在JMeter测试中使用断言的详细信息,请参阅“如何在三个简单步骤中使用JMeter断言”一文。

#1


2  

  1. Add JSON Extractor as a child of the request which produces the above JSON response and configure it as follows:

    将JSON Extractor添加为请求的子节点,该请求生成上述JSON响应并按如下方式对其进行配置:

    • Variable names: anything meaningful, i.e. week
    • 变量名称:任何有意义的东西,即周

    • JSON Path Expressions: $[*]
    • JSON路径表达式:$ [*]

    • Match No: -1

      比赛号:-1

      如何提取json路径并找到数组长度?

    This will produce the following JMeter Variables (you can validate them using Debug Sampler):

    这将生成以下JMeter变量(您可以使用Debug Sampler验证它们):

    week_1=["Week","Event Count"]
    week_2=["3\/13\/17","1"]
    week_3=["3\/20\/17","1"]
    week_4=["3\/27\/17","1"]
    week_5=["4\/3\/17","1"]
    week_6=["4\/10\/17","1"]
    week_7=["4\/17\/17","1"]
    week_matchNr=7
    

    You are particularly interested in the latter one

    你对后者特别感兴趣

    如何提取json路径并找到数组长度?

  2. Add Response Assertion as a child of the same request and configure it as follows:

    将Response Assertion添加为同一请求的子节点,并按如下方式对其进行配置:

    • Apply to: JMeter Variable -> week_matchNr
    • 适用于:JMeter变量 - > week_matchNr

    • Pattern Matching Rules: Equals
    • 模式匹配规则:等于

    • Patterns to Test: 7
    • 要测试的模式:7

    如何提取json路径并找到数组长度?

    This way your sampler will pass if number of matches will be equal to 7 and otherwise it will fail. See How to Use JMeter Assertions in Three Easy Steps article to learn more about using assertions in JMeter tests.

    这样,如果匹配数等于7,则采样器将通过,否则将失败。有关在JMeter测试中使用断言的详细信息,请参阅“如何在三个简单步骤中使用JMeter断言”一文。