JSR223 PreProcessor 在 Jmeter 中使用 Groovy 生成无效格式
JSR223 PreProcessor generates invalid format using Groovy in Jmeter
我的 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 数组。
更多信息:
我的 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 数组。
更多信息: