来自 JSON 文件的 Jmeter trim 空格
Jmeter trim whitespaces from a JSON File
我有以下Json:
{
"tipopersona": "M",
"regimen": "36",
"periodicidad": "Y",
"ejercicio": "2020",
"periodo": "035",
"periodoDesc": "Del Ejercicio",
"tipoDeclaracion": "001",
"tipoDeclaracionDesc": "Normal",
"tipoComplementaria": "",
"tipoComplementariaDesc": "",
"cmbISSIF": "1",
"obligaciones": [ "0101", "0192" ],
"preguntasPerfil": { "178": "1" },
"rfc": "AAC920317CM8",
"rechazopropuesta": false,
"fechaVencimiento": "2021-03-31T00:00:00",
"errores": true,
"xmldeclaracion": ""
}
我需要从 Json 中删除空格和换行符,就像 JSR223 或 Beanshell 中的以下示例:
{"tipopersona":"M","regimen":"36","periodicidad":"Y","ejercicio":"2020","periodo":"035","periodoDesc":"DelEjercicio","tipoDeclaracion":"001","tipoDeclaracionDesc":"Normal","tipoComplementaria":"","tipoComplementariaDesc":"","cmbISSIF":"1","obligaciones":["0101","0192"],"preguntasPerfil":{"178":"1"},"rfc":"AAC920317CM8","rechazopropuesta":false,"fechaVencimiento":"2021-03-31T00:00:00","errores":true,"xmldeclaracion":""}
我正在使用以下代码:
String data = vars.get("json");
data = data.replaceAll(" ", "");
data = data.replaceAll(System.getProperty("line.separator"),"");
vars.put("data", data);
但在 Debug Sampler 中它仍然显示空格:
enter image description here
最简单的方法是在 json 变量上使用 JSON 提取器作为 sampler.Your 代码的子级可能会替换 json 键和值中的所有空格.
- Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language 用于编写脚本
- Groovy has built-in JSON support
假设以上几点,您可以使用以下单行代码实现您的目标:
vars.put('data', (new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parseText(vars.get('json'))).toString()))
有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It
我有以下Json:
{
"tipopersona": "M",
"regimen": "36",
"periodicidad": "Y",
"ejercicio": "2020",
"periodo": "035",
"periodoDesc": "Del Ejercicio",
"tipoDeclaracion": "001",
"tipoDeclaracionDesc": "Normal",
"tipoComplementaria": "",
"tipoComplementariaDesc": "",
"cmbISSIF": "1",
"obligaciones": [ "0101", "0192" ],
"preguntasPerfil": { "178": "1" },
"rfc": "AAC920317CM8",
"rechazopropuesta": false,
"fechaVencimiento": "2021-03-31T00:00:00",
"errores": true,
"xmldeclaracion": ""
}
我需要从 Json 中删除空格和换行符,就像 JSR223 或 Beanshell 中的以下示例:
{"tipopersona":"M","regimen":"36","periodicidad":"Y","ejercicio":"2020","periodo":"035","periodoDesc":"DelEjercicio","tipoDeclaracion":"001","tipoDeclaracionDesc":"Normal","tipoComplementaria":"","tipoComplementariaDesc":"","cmbISSIF":"1","obligaciones":["0101","0192"],"preguntasPerfil":{"178":"1"},"rfc":"AAC920317CM8","rechazopropuesta":false,"fechaVencimiento":"2021-03-31T00:00:00","errores":true,"xmldeclaracion":""}
我正在使用以下代码:
String data = vars.get("json");
data = data.replaceAll(" ", "");
data = data.replaceAll(System.getProperty("line.separator"),"");
vars.put("data", data);
但在 Debug Sampler 中它仍然显示空格:
enter image description here
最简单的方法是在 json 变量上使用 JSON 提取器作为 sampler.Your 代码的子级可能会替换 json 键和值中的所有空格.
- Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language 用于编写脚本
- Groovy has built-in JSON support
假设以上几点,您可以使用以下单行代码实现您的目标:
vars.put('data', (new groovy.json.JsonBuilder(new groovy.json.JsonSlurper().parseText(vars.get('json'))).toString()))
有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It