读取 json 文件作为 json 字符串并在 jsr223 采样器中提取数据,groovy 作为 jmeter 中的语言
Reading json file as json string and extracting data in jsr223 sampler, groovy as language in jmeter
import com.jayway.jsonpath.JsonPath
def pathToRead = someFilePath;
def pathToWrite = someOtherFilePath;
def newLine = System.getProperty('line.separator')
def index = [];
def randoms = [];
def temp;
//generating random numbers
def size = new File(pathToRead + "index.csv").readLines().size();
for(int i=0; i<vars.get('extractCount'); i++)
{
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
}
//Reading file names to extract data
File file = new File(pathToRead + "index.csv");
file.each { line ->
index << line
}
def nameCSV = new File(pathToWrite + 'name.csv')
def nameGivenCSV = new File(pathToWrite + 'given.csv')
def givenList = []
def nameFamilyCSV = new File(pathToWrite + 'family.csv')
def familyList = []
//going through each json file and extracting data and storing in lists
randoms.unique().each { random ->
jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
def names = JsonPath.read(jsonString, '$..name')
names.each { name ->
name.each { subName ->
subName.get('given').each { givenName ->
if(givenName != null)
givenList << givenName
}
temp = subName.get('family')
if(temp != null)
familyList << temp
}
}
}
//Writing data to files after removing the duplicates
givenList.unique().each { single_given ->
nameCSV << single_given << newLine
nameGivenCSV << single_given << newLine
}
familyList.unique().each { single_family ->
nameCSV << single_family << newLine
nameFamilyCSV << single_family << newLine
}
这是给出错误的说法
JSR223 脚本 ExtractRandomData 中的问题,消息:javax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:java.lang.String.get() 适用于参数类型:(java.lang.String) 值:[给定]
这意味着它不让我申请 subName.get('given');
文件中的json数据就像
"name": [
{
"use": "official",
"family": "Cortez851",
"given": [
"Benito209"
],
"prefix": [
"Mr."
]
}
]
您的代码与您提供的 JSON 数据相匹配,应该可以正常工作。但是,查看某些文件的结构不同的错误。
您可以通过显式检查您正在使用 instanceof 运算符的 subname
类型来解决此问题,例如:
names.each { name ->
name.each { subName ->
if (subName instanceof Map) {
subName.get('given').each { givenName ->
if (givenName != null)
givenList << givenName
}
} else if (subName instanceof String) {
givenList << subName
} else {
log.error('Expected Map or String, got' + subName.getClass().getName())
}
temp = subName.get('family')
if (temp != null)
familyList << temp
}
}
演示:
其他改进:
你似乎根本没有使用 JsonSlurper 所以你可以简化这一行:
jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
到这个
jsonString = new File(pathToRead + "Data/fhir/" + index.getAt(随机)).text
这一行永远不会访问index.csv
中的最后一个值
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
你应该把它改成
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size);
您可以将您的代码放在一个全局 try block 中并将有问题的 JSON 打印到 jmeter.log 文件中失败案例
更多信息:
import com.jayway.jsonpath.JsonPath
def pathToRead = someFilePath;
def pathToWrite = someOtherFilePath;
def newLine = System.getProperty('line.separator')
def index = [];
def randoms = [];
def temp;
//generating random numbers
def size = new File(pathToRead + "index.csv").readLines().size();
for(int i=0; i<vars.get('extractCount'); i++)
{
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
}
//Reading file names to extract data
File file = new File(pathToRead + "index.csv");
file.each { line ->
index << line
}
def nameCSV = new File(pathToWrite + 'name.csv')
def nameGivenCSV = new File(pathToWrite + 'given.csv')
def givenList = []
def nameFamilyCSV = new File(pathToWrite + 'family.csv')
def familyList = []
//going through each json file and extracting data and storing in lists
randoms.unique().each { random ->
jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
def names = JsonPath.read(jsonString, '$..name')
names.each { name ->
name.each { subName ->
subName.get('given').each { givenName ->
if(givenName != null)
givenList << givenName
}
temp = subName.get('family')
if(temp != null)
familyList << temp
}
}
}
//Writing data to files after removing the duplicates
givenList.unique().each { single_given ->
nameCSV << single_given << newLine
nameGivenCSV << single_given << newLine
}
familyList.unique().each { single_family ->
nameCSV << single_family << newLine
nameFamilyCSV << single_family << newLine
}
这是给出错误的说法 JSR223 脚本 ExtractRandomData 中的问题,消息:javax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:java.lang.String.get() 适用于参数类型:(java.lang.String) 值:[给定]
这意味着它不让我申请 subName.get('given');
文件中的json数据就像
"name": [
{
"use": "official",
"family": "Cortez851",
"given": [
"Benito209"
],
"prefix": [
"Mr."
]
}
]
您的代码与您提供的 JSON 数据相匹配,应该可以正常工作。但是,查看某些文件的结构不同的错误。
您可以通过显式检查您正在使用 instanceof 运算符的 subname
类型来解决此问题,例如:
names.each { name ->
name.each { subName ->
if (subName instanceof Map) {
subName.get('given').each { givenName ->
if (givenName != null)
givenList << givenName
}
} else if (subName instanceof String) {
givenList << subName
} else {
log.error('Expected Map or String, got' + subName.getClass().getName())
}
temp = subName.get('family')
if (temp != null)
familyList << temp
}
}
演示:
其他改进:
你似乎根本没有使用 JsonSlurper 所以你可以简化这一行:
jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
到这个 jsonString = new File(pathToRead + "Data/fhir/" + index.getAt(随机)).text
这一行永远不会访问
中的最后一个值index.csv
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
你应该把它改成
randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size);
您可以将您的代码放在一个全局 try block 中并将有问题的 JSON 打印到 jmeter.log 文件中失败案例
更多信息: