如何在单个请求中发送多个 JSON(Jmeter)

How to send multiple JSON in single request(Jmeter)

虽然我可以看到这个问题可能会重复,但找​​不到以下 JSON 支柱的任何类似解决方案。请建议。

我有 excel sheet 列中的数据如下所示: CSV file data

我的预期 JSON 为:

       {
       "Child ": {
       "10"
          : { "Post": { "Kid-R":1 },
        "Var": [1,1 ],
        "Tar": [2,2],
        "Fur": [3,3]},

       "11": 
          {"Post": {"Kid-R":2 },
          "Var": [1,1 ],
          "Tar": [2,2 ],
          "Fur": [5,4 ]}
          },
         "Clone": [],
         "Birth": 2,
         "TT": 11,
         "Clock": ${__time(/1000,)}
           }

我已经尝试在 JMeter 中加入 beanshell 预处理器并尝试了以下代码:

    def builder = new groovy.json.JsonBuilder()
    @groovy.transform.Immutable
    class Child {
    String post
    String var
    String Tar
    String Fur
    }


    def villas = new File("Audit_27.csv")
    .readLines()
    .collect { line ->
        new child (line.split(",")[1],(line.split(",") 
    [2]+","+line.split(",")[3]),(line.split(",")[4]+","+line.split(",") 
   [5]),(line.split(",")[6]+","+line.split(",")[7]))}     

    builder(

    Child :villas.collect(),
         "Clone": [],
         "Birth": 2,
         "TT": 11,
         "Clock": ${__time(/1000,)}

    )
     log.info(builder.toPrettyString())
     vars.put("payload", builder.toPrettyString())

我只能看到以下回复:

注意:我不知道如何在上述解决方案中声明 "Key" 值 (line.split(",")[0])。

      {
     "Child": [
      {
        "post": "\"\"\"Kid-R\"\":1\"",
        "var": "\"[2,2]\"",
        "Tar": "\"[1,1]\"",
        "Fur": "\"[3,3]\""
    },
    {
        "post": "\"\"\"Kid-R\"\":2\"",
        "var": "\"[2,2]\"",
        "Tar": "\"[1,1]\"",
        "Fur": "\"[3,3]\""
      }
     ],
     "Clone": [],
     "Birth": 2,
     "TT": 11,
     "CLock": 1585219797
      }

如有任何帮助,我们将不胜感激

您正在复制和粘贴 中的解决方案,但并未理解您在做什么。

如果您将 class 名称从 VILLA 更改为 own,您需要使用 new own 而不是 new VILLA

此行也不会编译:Clock: <take system current time> 您需要使用 System.currentTimeMillis() or appropriate function of the Date class 才能生成时间戳。

如果你想要一个全面的答案,你需要提供:

  1. 格式正确的 CSV 文件
  2. 有效JSON有效载荷

同时,我建议您熟悉以下内容 material:

  1. Apache Groovy: Parsing and producing JSON
  2. Apache Groovy - Why and How You Should Use It
  3. Reading a File in Groovy

实际上我将遵循 DmirtiT 的建议,正如 post 中提到的那样,使用随机变量进行批量 API 请求。同样的答案它也帮助我在这里生成具有唯一数据的多个 JSON 结构。谢谢..