JMeter无法通过使用fastjson JSONObject来保持JSON顺序,parseObject

vsnjm48y  于 2022-11-19  发布在  SEO
关注(0)|答案(1)|浏览(576)

背景:

在Jmeter中,我需要获取json post数据,然后解析到JSONObject
但是键的顺序应该相同
json发布的数据为:{“一”:“一”,“二”:“二”、“三”:“三”、“四”:“4”}
在JSON对象解析之后,它还应该是:{“一”:“一”,“二”:“二”、“三”:“三”、“四”:“4”}

解决方案:

在纯java中,我可以使用下面的代码来保持顺序

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

String requestBody = "{ \"one\": \"1\", \"two\": \"2\", \"three\": \"3\", \"four\": \"4\" }";
log.info("********************requestBody: " + requestBody);

// Expected: keep the JSONObject key oder same as the body data
// But following code not working in JMeter JSR223 Sampler, it works fine in pure JAVA
JSONObject maps = JSONObject.parseObject(requestBody, Feature.OrderedField);
log.info("********************maps expected: " + maps);

出版日期:

但是上面的代码在JMETER JSR 223 Sampler中不起作用,我得到以下错误:

Static method parseObject( java.lang.String, com.alibaba.fastjson.parser.Feature ) not found in class'com.alibaba.fastjson.JSONObject'...

复制:

将以下代码保存为.jmx文件,以便在JMeter中重现
你需要下载fastjson 1.2.83,并放到jmeter lib文件夹中:https://mvnrepository.com/artifact/com.alibaba/fastjson/1.2.83

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">1</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="JSON Sort Issue" enabled="true">
          <boolProp name="HTTPSampler.postBodyRaw">true</boolProp>
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments">
            <collectionProp name="Arguments.arguments">
              <elementProp name="" elementType="HTTPArgument">
                <boolProp name="HTTPArgument.always_encode">false</boolProp>
                <stringProp name="Argument.value">{&#xd;
  &quot;one&quot;: &quot;1&quot;,&#xd;
  &quot;two&quot;: &quot;2&quot;,&#xd;
  &quot;three&quot;: &quot;3&quot;,&#xd;
  &quot;four&quot;: &quot;4&quot;&#xd;
}</stringProp>
                <stringProp name="Argument.metadata">=</stringProp>
              </elementProp>
            </collectionProp>
          </elementProp>
          <stringProp name="HTTPSampler.domain">httpbin.org</stringProp>
          <stringProp name="HTTPSampler.port"></stringProp>
          <stringProp name="HTTPSampler.protocol">https</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/post</stringProp>
          <stringProp name="HTTPSampler.method">POST</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree>
          <JSR223PreProcessor guiclass="TestBeanGUI" testclass="JSR223PreProcessor" testname="JSR223 预处理程序" enabled="true">
            <stringProp name="cacheKey">true</stringProp>
            <stringProp name="filename"></stringProp>
            <stringProp name="parameters"></stringProp>
            <stringProp name="script">import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

String requestBody = sampler.getArguments().getArgument(0).getValue();
log.info(&quot;********************requestBody: &quot; + requestBody);

// UnExpected: key order is not same as the body data
JSONObject maps = JSONObject.parseObject(requestBody);
log.info(&quot;********************maps unexpected: &quot; + maps);

// Expected: keep the JSONObject key oder same as the body data
// But following code not working in JMeter JSR223 Sampler, it works fine in pure JAVA
JSONObject maps = JSONObject.parseObject(requestBody, Feature.OrderedField);
log.info(&quot;********************maps expected: &quot; + maps);
</stringProp>
            <stringProp name="scriptLanguage">beanshell</stringProp>
          </JSR223PreProcessor>
          <hashTree/>
        </hashTree>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>
uqcuzwp8

uqcuzwp81#

首先,您在同一作用域中声明了两次maps变量,这根本不是有效代码。
回到你的问题:只需将语言更改为groovy,即可解决您问题。

从JMeter 3.1开始,推荐使用Groovy语言编写脚本,主要是因为Groovy performance is much better comparing the other scripting options。此外,Groovy更好地支持Java,而Beanshell不支持任何Java 5+特性。

相关问题