在 jmeter 的 jsr223 采样器中读取 json 文件并提取数据

Read json file in jsr223 sampler in jmeter and extract data

import com.jayway.jsonpath.JsonPath

def idCSV = new File('id.csv')
def index = [fileOne.json, fileTwo.json]

def jsonString

index.each { file ->
 jsonString = ________

 def ids = JsonPath.read(jsonString, '$..id')

 ids.each { id ->
  idCSV << id << newLine
 }
}

如何填充 jsonString = ____,以便我可以 json 将文件转换为字符串并解析字符串以从 json 中提取 ID 和一些信息] 细绳。 而且我不以 http 请求-> GET-> 文件格式来做。 以前我已经从 http 响应中提取了 jsonString,它运行良好,现在我想这样做。

使用JsonSlurper:

def jsonString = new groovy.json.JsonSlurper().parseText(new File("json.txt").text)

我的预期是您正在寻找 File.getText() 函数

jsonString = file.text

我不明白为什么你需要将 JSON 中的值存储在 CSV 文件中,但是有另一种方法可以实现这一点,它不需要脚本,因为你的方法可以使用只有 1 个并发线程,如果您要添加更多尝试写入同一文件的用户 - 您将 运行 变成 race condition :

  • 您可以通过 Directory Listing Config
  • 将文件夹中的文件读入 JMeter 变量
  • 可以使用HTTP Request采样器读取文件

  • 可以使用 JSON Extractor 获取的值,它们将自动存储到 JMeter 变量中,以便您稍后可以使用它们
  • 如果您需要在文件中显示这些值(尽管我不推荐这种方法,因为它会导致大量磁盘 IO,并且可能 运行 您的测试),您可以选择 Flexible File Writer