简体   繁体   English

Jmeter中的JSON Extractor中的长度不起作用

[英]Length is not worked in JSON Extractor in Jmeter

I need to get the count of card from json file. 我需要从json文件中获取卡的数量。 For this I've used $.storedCards.cards.lenght in JSON Extractor but it doesn't work. 为此,我在JSON Extractor中使用了$.storedCards.cards.lenght ,但它不起作用。 There is an error message: 有一条错误消息:

Options AS_PATH_LIST and ALWAYS_RETURN_LIST are not allowed when using path functions! 使用路径功能时,不允许选项AS_PATH_LIST和ALWAYS_RETURN_LIST!

After that I've tried JSR223 PostProcessor with next script on goovy 之后我在goovy上尝试了下一个脚本的JSR223 PostProcessor

 def jsonText = '''${AllCards}''' //${AllCards} has json value
 def json = new JsonSlurper().parseText(jsonText)
 log.info( "Json length---------->"+json.resource.size())
 ${CardsCount} = props.get("4") //vars.put(json.resource.size.toString())

but there is problem with set value to my variable. 但是我的变量的设定值存在问题。 Or when i've created variable in Groovy it was impossible to use outside from script. 或者当我在Groovy中创建变量时,无法在脚本外部使用。

My json file 我的json文件

    "storedCards":
    {
        "cards":
        [
            {
                "CardId":"123",
                "cardBrand":"Visa",
                "lastFourDigits":"2968",
               },
            {
                "CardId":"321",
                "cardBrand":"Visa",
                "lastFourDigits":"2968",
              },
     ..........
        ],

How can i get the count of card and set to my Variables? 我如何获得卡的数量并设置为我的变量? what should i use for this? 我该怎么用?

Your JSON data seems to be invalid. 您的JSON数据似乎无效。 Assuming you have the valid JSON like below, I'm answering your question. 假设你有如下的有效JSON,我正在回答你的问题。

{
    "storedCards": {
        "cards": [
            {
                "CardId": "123",
                "cardBrand": "Visa",
                "lastFourDigits": "2968"
            },
            {
                "CardId": "321",
                "cardBrand": "Visa",
                "lastFourDigits": "2968"
            }
        ]
    }
}

You dont need to write Groovy code, you can resolve this using JSON Extractor . 您不需要编写Groovy代码,您可以使用JSON Extractor解决此问题。 Instead of using length function, use JSON path predicate like this- 而不是使用长度函数,使用像这样的JSON路径谓词 -

$.storedCards.cards[*]

Though Variable you used in JSON Extractor won't give the solution right away, another JMeter function helps - __RandomFromMultipleVars 虽然您在JSON Extractor中使用的变量不会立即提供解决方案,但另一个JMeter函数有帮助 - __RandomFromMultipleVars

Excerpt from documentation - 摘自文件 -

The RandomFromMultipleVars function returns a random value based on the variable values provided by Source Variables. RandomFromMultipleVars函数根据Source Variables提供的变量值返回随机值。 The variables can be simple or multi-valued as they can be generated by the following extractors: 变量可以是简单的或多值的,因为它们可以由以下提取器生成:

 Boundary Extractor Regular Expression Extractor CSS Selector Extractor JSON Extractor XPath Extractor XPath2 Extractor 

Multi-value vars are the ones that are extracted when you set -1 for Match Numbers. 多值变量是在为匹配数字设置-1时提取的变量。 This leads to creation of match number variable called varName_matchNr and for each value to the creation of variable varName_n where n = 1, 2, 3 etc. 这导致创建名为varName_matchNr的匹配编号变量,并为每个值创建变量varName_n,其中n = 1,2,3等。

So once you use the predicate, you will get the count in the yourVariableName_matchNr . 因此,一旦使用谓词,您将获得yourVariableName_matchNr中的计数。 Example:- 例:-

在此输入图像描述

在此输入图像描述

Hope this help. 希望这有帮助。

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

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