简体   繁体   English

如何将 JSON 数组对象解析为 JMeter 中的字符串数组对象

[英]How to parse JSON array objects to string array objects in JMeter

I have the following JSON.我有以下 JSON。 I want the JSON array objects of technicalSettings (two objects in this case but can vary based on the API response) into a string array without loosing any text and want to loop through the string array to add few more elements and to form a new JSON and store the new JSON in a string variable.我想要技术设置的 JSON 数组对象(在这种情况下是两个对象,但可能会根据 API 响应而有所不同)到一个字符串数组中而不丢失任何文本,并希望循环通过字符串数组添加更多元素并形成一个新的 Z0ECD1F8D148并将新的 JSON 存储在字符串变量中。

{
  "data": {
    "statusCode": 200,
    "success": true,
    "technicalSettings": [
      {
        "program": "C:/temp/abc.exe",
        "actions": "9",
        "file_name": "abc1",
        "new_file_name": "newabc1",
        "version": "2.0.0.0",
        "product_name": "abc",
        "description": "abc",
        "eventdate": "20160601120000",
        "autoVoiceProfile": {
              "autoVoices": [
                {
                  "autoVoiceLanguage": 0,
                  "autoVoiceMessage": [
                    {
                      "name": "AV1",
                      "duration": "1.200000",
                      "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV1",
                      "duration": "0.600000",
                      "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV2",
                      "duration": "2.800000",
                      "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                      "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                      "id": 5
                    },
                    {
                      "name": "AV2",
                      "duration": "4.100000",
                      "checksum": "c5a6a39df38505c0c22b75d9ea7781a1755e9c8c9f435e08034f579361ba751c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg10.aifc",
                      "id": 5
                    }
                  ]
                }
              ],
              "messagesitefilename": null
            }
        
      },
      {
        "program": "C:/temp/abc.exe",
        "actions": "9",
        "file_name": "abc2",
        "new_file_name": "newabc2",
        "version": "2.0.0.0",
        "product_name": "abc",
        "description": "abc",
        "eventdate": "20160601120000",
        "autoVoiceProfile": {
              "autoVoices": [
                {
                  "autoVoiceLanguage": 0,
                  "autoVoiceMessage": [
                    {
                      "name": "AV1",
                      "duration": "1.200000",
                      "checksum": "2d4c44d142bc0391b980b8a103ab35cc23d8f7820895cb6025cf3c829139336c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg7.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV1",
                      "duration": "0.600000",
                      "checksum": "9538cf287d178964dcb57a05b7acbc00e04c800a9aaed0b22f5433d9dc79d80c",
                      "fileName": "/usr/g/db/user_autoVoiceMsg8.aifc",
                      "id": 4
                    },
                    {
                      "name": "AV2",
                      "duration": "2.800000",
                      "checksum": "050acdb345e079da1371623c9727bc16d166db0a0b47687ff93d736ddf37cde8",
                      "fileName": "/usr/g/db/user_autoVoiceMsg9.aifc",
                      "id": 5
                    }
                  ]
                }
              ],
              "messagesitefilename": null
            }
      }
    ],
    "library": {
      "version": 6,
      "dmIdVersion": 5
    }
  },
  "success": true,
  "statusCode": 200,
  "errorMessage": ""
}

I used the JSON Extractor but it is failing when split into array since the array objects contains multiple ",".我使用了 JSON 提取器,但由于数组对象包含多个“,”,因此在拆分为数组时失败。

String strPublishTechSettings = "${pPublishTechSettings_ALL}";字符串 strPublishTechSettings = "${pPublishTechSettings_ALL}";

String[] PublishTechSettings = strPublishTechSettings.split(","); String[] PublishTechSettings = strPublishTechSettings.split(",");

在此处输入图像描述

Don't inline JMeter Functions or Variables into scripts as:不要将JMeter 函数或变量内联到脚本中:

  • in case of compilation caching enabled only first value will be used for all iterations在启用编译缓存的情况下,只有第一个值将用于所有迭代
  • it conflicts with Groovy GString Template Feature它与Groovy GString 模板功能冲突
  • it might be resolved into something causing compilation failure or unexpected behaviour它可能会被解析为导致编译失败或意外行为的东西

so change this line:所以改变这一行:

String strPublishTechSettings = "${pPublishTechSettings_ALL}";

to this one:对此:

String strPublishTechSettings = "${pPublishTechSettings_ALL}";

and your test should start working as expected并且您的测试应该按预期开始工作在此处输入图像描述 :

in the above example vars stands for JMeterVariables class instance, see JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on other JMeter API shorthands available to JSR223 Test Elements in the above example vars stands for JMeterVariables class instance, see JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on other JMeter API shorthands available to JSR223 Test Elements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM