用于Jmeter上post方法的beanShell json体

时间:2016-01-13 03:50:25

标签: beanshell

任何人都可以帮助添加带有像这个儿子身上的斜杠的转义引号:

    {
  "firstName": "teo",
  "lastName": "leo",
  "companyName": "abc",
  "restaurantId": "54d34443e4b0382b3208703d",
  "phones": [
    {
      "label": "Mobile",
      "value": "123456789",
      "countryCode": "+123",
      "isPrimary": true
    }
  ],
  "addresses": "haha"
}

我尝试过这个,但beanShell PreProcessor无法接受

String formvalues = "{\"firstName\": \"teo\",\"lastName\": \"leo\",\"companyName\": \"abc\",\"restaurantId\": \"54d34443e4b0382b3208703d\",\"phones\": [{\"label\":\"Mobile\",\"value\": \"123456789\",\"countryCode\": \"+123\",\"isPrimary\": true}],\"addresses\": \"haha\"}"

非常感谢你!

1 个答案:

答案 0 :(得分:0)

如果你想保持格式化:

String formvalues = "{\n" +
        "  \"firstName\": \"teo\",\n" +
        "  \"lastName\": \"leo\",\n" +
        "  \"companyName\": \"abc\",\n" +
        "  \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
        "  \"phones\": [\n" +
        "    {\n" +
        "      \"label\": \"Mobile\",\n" +
        "      \"value\": \"123456789\",\n" +
        "      \"countryCode\": \"+123\",\n" +
        "      \"isPrimary\": true\n" +
        "    }\n" +
        "  ],\n" +
        "  \"addresses\": \"haha\"\n" +
        "}";

如果您想要单行(请注意Content-Length会有所不同)

String formvalues = "{\"firstName\":\"teo\",\"lastName\":\"leo\",\"companyName\":\"abc\",\"restaurantId\":\"54d34443e4b0382b3208703d\",\"phones\":[{\"label\":\"Mobile\",\"value\":\"123456789\",\"countryCode\":\"+123\",\"isPrimary\":true}],\"addresses\":\"haha\"}";

生成正文并将其添加为参数的完整代码:

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;

String formvalues = "{\n" +
           "  \"firstName\": \"teo\",\n" +
           "  \"lastName\": \"leo\",\n" +
           "  \"companyName\": \"abc\",\n" +
           "  \"restaurantId\": \"54d34443e4b0382b3208703d\",\n" +
           "  \"phones\": [\n" +
           "    {\n" +
           "      \"label\": \"Mobile\",\n" +
           "      \"value\": \"123456789\",\n" +
           "      \"countryCode\": \"+123\",\n" +
           "      \"isPrimary\": true\n" +
           "    }\n" +
           "  ],\n" +
           "  \"addresses\": \"haha\"\n" +
           "}";

Arguments arguments = new Arguments();
arguments.addArgument(new HTTPArgument("",formvalues));       
sampler.setArguments(arguments);

相关课程上的JavaDoc:

有关JMeter中Beanshell脚本的更多信息,请参阅How to Use BeanShell: JMeter's Favorite Built-in Component指南。