首页 > 解决方案 > JSR223 PreProcessor 在 Jmeter 中使用 Groovy 生成无效格式

问题描述

我的 Groovy 脚本看起来像

def statuses = []
def item = []
def items = []
def payload = []

1.upto(1, { index ->
    def data = new File('/Users/my/Desktop/items.csv').readLines().get(index).split(',')
    def a = [:]
        a.put('1', 5)
    a.put('2', 4)
    statuses.add(a)
    def attr = [:]
    attr.put('3', data[3].toInteger())
    def attributes = [:]
    attributes.put('4', statuses)
    attributes.put('5', attr)
    item.add(attributes)
})

1.upto(1, { index ->
    def data = new File('/Users/my/Desktop/locations.csv').readLines().get(index).split(',')
    def attr = [:]
    attr.put('6', item)
    attr.put('7', data[1].toInteger())
    items.add(attr)
    def attributes = [:]
    attributes.put('param', items)
    payload.add(attributes)

})

def json = new groovy.json.JsonBuilder(payload)

sampler.addNonEncodedArgument("",json.toPrettyString(),"")
sampler.setPostBodyRaw(true)

结果是我的 http 请求的有效负载

[
    {
        "param": [
            {
                "6": [
                    {
                        "4": [
                            {
                                "1": 5,
                                "2": 4
                            }
                        ],
                        "5": {
                            "3": 101
                        }
                    }
                ],
                "7": 20
            }
        ]
    }
]

问题是当我准备 JsonBuilder(payload) 时,def payload 是数组 (def payload = []) 结果我有那些​​嵌套格式

[
    {
        "param"

我应该怎么做才能使我的有效负载嵌套没有 [] 括号?像这样:

{
   "param"
    ...

标签: groovyjmeterpreprocessor

解决方案


  1. def payload = [:]
  2. payload.put('param', your_request_body)

您需要使用LazyMap来生成 JSON 对象,而不是 JSON 数组。

更多信息:


推荐阅读