groovy - 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"
...
解决方案
def payload = [:]
payload.put('param', your_request_body)
您需要使用LazyMap来生成 JSON 对象,而不是 JSON 数组。
更多信息:
推荐阅读
- python - 实现所需的类层次结构行为的最佳方法
- typescript - 如何仅针对特定 ID 使用 SWR 进行变异?
- linux - 如何使用 bash 在一次读取操作中组合两个 STDOUT 消息流?
- javascript - Vuejs 默认对象不更新 dom 数据
- flutter - Flutter:有没有一种更简洁的方法可以无限滚动 Provider 属性?
- excel - VBA从excel创建word文档:Word文档未打开
- vue.js - 购物车vuejs中的反应性
- javascript - 如何在 discord.js 中邀请某人加入公会?
- xcode - 如何修复 SwiftUI tvOS ScrollView 跳跃/抖动/片状/错误/故障行为?
- php - GoDaddy 上的 php_com_dotnet 扩展?