Jmeter json 提取并删除重复项以写入文件
Jmeter json extraction and removing the duplicates to write into a file
import com.jayway.jsonpath.JsonPath
def path = vars.get("BaseFilePath") + "/" + vars.get("FhirVersion") + "/Get/Patient/";
def newLine = System.getProperty('line.separator')
def response = prev.getResponseDataAsString()
//address
def addressCSV = new File(path + 'address.csv')
def addressList = []
def addressCityCSV = new File(path + 'address-city.csv')
def cityList = []
def addressCountryCSV = new File(path + 'address-country.csv')
def countryList = []
def addressPostalCodeCSV = new File(path + 'address-postalcode.csv')
def postalCodeList = []
def addressStateCSV = new File(path + 'address-state.csv')
def stateList = []
def addressArray = JsonPath.read(response, '$..address')
addressArray.each { eachAddress ->
eachAddress.each { subAddress ->
subAddress.get('line').each { line ->
addressList.add(line)
}
cityList.add(subAddress.get('city'))
stateList.add(subAddress.get('state'))
postalCodeList.add(subAddress.get('postalCode'))
countryList(subAddress.get('country'))
}
addressList.unique().each { address ->
addressCSV << address << newLine
}
cityList.unique().each { city ->
addressCityCSV << city << newLine
}
countryList.unique().each { country ->
addressCountryCSV << country << newLine
}
postalCodeList.unique().each { postalCode ->
addressPostalCodeCSV << postalCode << newLine
}
stateList.unique().each { state ->
addressStateCSV << state << newLine
}
}
我在 jmeter 的 JSR223 post 处理器中编写了这个脚本,以从 json 响应中提取数据,addressList、cityList 和其他列表包含重复元素,所以我想删除重复项并推送唯一值写入文件。
但是这段代码不起作用。谁能帮我解决这个问题
要仅保留唯一值,请使用 Set
We can use the toSet() function to convert a List to a Set.
因此您可以将其添加到相关列表中:
uniqueStateCSVSet = addressStateCSV.toSet()
或者预定义,如
Set addressArray
调用 Collection.unique() 函数应该可以解决问题,即
addressList = addressList.unique()
演示:
但是,如果您的集合包含自定义对象(即非正常 Strings) you will need to come up with a special Comparator 实现,即
def myComparator = [
equals: { delegate.equals(it) },
compare: { source, target ->
source.someField <=> target.someField
}
] as Comparator
def unique = addressList.unique(myComparator)
有关更多 Groovy 提示和技巧,请参阅 The Groovy Templates Cheat Sheet for JMeter
import com.jayway.jsonpath.JsonPath
def path = vars.get("BaseFilePath") + "/" + vars.get("FhirVersion") + "/Get/Patient/";
def newLine = System.getProperty('line.separator')
def response = prev.getResponseDataAsString()
//address
def addressCSV = new File(path + 'address.csv')
def addressList = []
def addressCityCSV = new File(path + 'address-city.csv')
def cityList = []
def addressCountryCSV = new File(path + 'address-country.csv')
def countryList = []
def addressPostalCodeCSV = new File(path + 'address-postalcode.csv')
def postalCodeList = []
def addressStateCSV = new File(path + 'address-state.csv')
def stateList = []
def addressArray = JsonPath.read(response, '$..address')
addressArray.each { eachAddress ->
eachAddress.each { subAddress ->
subAddress.get('line').each { line ->
addressList.add(line)
}
cityList.add(subAddress.get('city'))
stateList.add(subAddress.get('state'))
postalCodeList.add(subAddress.get('postalCode'))
countryList(subAddress.get('country'))
}
addressList.unique().each { address ->
addressCSV << address << newLine
}
cityList.unique().each { city ->
addressCityCSV << city << newLine
}
countryList.unique().each { country ->
addressCountryCSV << country << newLine
}
postalCodeList.unique().each { postalCode ->
addressPostalCodeCSV << postalCode << newLine
}
stateList.unique().each { state ->
addressStateCSV << state << newLine
}
}
我在 jmeter 的 JSR223 post 处理器中编写了这个脚本,以从 json 响应中提取数据,addressList、cityList 和其他列表包含重复元素,所以我想删除重复项并推送唯一值写入文件。 但是这段代码不起作用。谁能帮我解决这个问题
要仅保留唯一值,请使用 Set
We can use the toSet() function to convert a List to a Set.
因此您可以将其添加到相关列表中:
uniqueStateCSVSet = addressStateCSV.toSet()
或者预定义,如
Set addressArray
调用 Collection.unique() 函数应该可以解决问题,即
addressList = addressList.unique()
演示:
但是,如果您的集合包含自定义对象(即非正常 Strings) you will need to come up with a special Comparator 实现,即
def myComparator = [
equals: { delegate.equals(it) },
compare: { source, target ->
source.someField <=> target.someField
}
] as Comparator
def unique = addressList.unique(myComparator)
有关更多 Groovy 提示和技巧,请参阅 The Groovy Templates Cheat Sheet for JMeter