常规JSON构建器需要删除空格

时间:2018-08-30 18:39:08

标签: groovy jmeter jsr223

我正在尝试使用Groovy JSON构建器构建JSON。 在JSR223中用作脚本的代码如下: 使用的代码:

**def previousResponse = new groovy.json.JsonSlurper().parse(ctx.getPreviousResult().getResponseData())
@groovy.transform.Immutable
class Package {
    String ReferenceId
    String Name
    String Version
    Integer Size
}
def packages = previousResponse.Packages.collect { Package -> new Package(Package.ReferenceId, Package.Name, Package.LastVersion.Version, Package.LastVersion.Size) }
def builder = new groovy.json.JsonBuilder()
builder(HasDraftVersion: false,
        IsApprovalPending: null,
        IsInstalledSilently: true,
        LastModified: "/Date(1535570626907)/",
        LastModifiedBy: "Administrator",
        DeviceConfigurations: [],
        Packages: packages.collect(),
        LdapTargets: null,
        ReferenceId: null,
        DeviceFamily: 6,
        DeviceFamilyQualification: 0,
        Name: "test",
        Description: "",
        Random: "2018-08-29T15:16:47",
        IsPublicAPI: true)
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toString(),'')
sampler.setPostBodyRaw(true);        

The result I am getting from the above code is correct and is in the format:

{“ HasDraftVersion”:false,“ IsApprovalPending”:null,“ IsInstalledSilently”:true,“ LastModified”:“ / Date(1535661532840)/”,“ LastModifiedBy”:“ Administrator”,“ DeviceConfigurations”: [],“包裹”:[{“ referenceId”:“ 282812e7-c3e6-4ecb-8267-bbe2c250ad59”,“版本”:“ 1.0”,“大小”:10465294,“名称”:“ 10MB”,“ packageDependencies” :“”},{“ referenceId”:“ 02f5b531-8e37-42c1-bfcb-ec8db0920bed”,“版本”:“ 1.0”,“大小”:104884390,“名称”:“ 100MB”,“ packageDependencies”:“” }],“ LdapTargets”:null,“ ReferenceId”:null,“ DeviceFamily”:6,“ DeviceFamilyQualification”:0,“ Name”:“ work”,“ Description”:“”,“ Random”:“ 2018-08 -30T04:38:52“,” IsPublicAPI“:true}

我面临的问题是标签的顺序和像Packages标签的顺序这样的值应该是:

ReferenceID,名称,版本PackageDependencies

当我将其传递给下一个HTTP请求时遇到的错误是

{“ Data”:null,“ ErrorCode”:0,“ ErrorMessage”:“参数'ReferenceId'具有无效值”。“,” HelpLink“:null,” InnerErrors“:[{” Data “:[” ReferenceId“,”“],” ErrorCode“:2,” ErrorMessage“:”参数'ReferenceId'具有无效值“。”,“ HelpLink”:null,“ InnerErrors”:null,“ Ref”: “” 2018-08-30 16:38:52.9245-04:00“}],” Ref“:” 2018-08-30 16:38:52.9245-04:00“}

正在传递ReferenceId,结果在那里。

1 个答案:

答案 0 :(得分:0)

看起来像Groovy is converting maps names匹配naming convention

因此,为了保留这些ReferenceID参数和朋友,而不是在驼峰情况下,您需要修改此行:

Packages: packages.collect()

与此:

Packages: packages.collect() {
     [
             ReferenceId        : it.ReferenceId,
             Name               : it.Name,
             Version            : it.Version,
             PackageDependencies: "",
             Size               : it.Size
     ]
 },

完整代码以防万一:

def previousResponse = new groovy.json.JsonSlurper().parse(ctx.getPreviousResult().getResponseData())

@groovy.transform.Immutable
class Package {
    String ReferenceId
    String Name
    String Version
    Integer Size
}

def packages = previousResponse.Packages.collect { Package -> new Package(Package.ReferenceId, Package.Name, Package.LastVersion.Version, Package.LastVersion.Size) }

def builder = new groovy.json.JsonBuilder()

builder(HasDraftVersion: false,
        IsApprovalPending: null,
        IsInstalledSilently: true,
        LastModified: "/Date(1535570626907)/",
        LastModifiedBy: "Administrator",
        DeviceConfigurations: [],
        Packages: packages.collect() {
            [
                    ReferenceId        : it.ReferenceId,
                    Name               : it.Name,
                    Version            : it.Version,
                    PackageDependencies: "",
                    Size               : it.Size
            ]
        },
        LdapTargets: null,
        ReferenceId: null,
        DeviceFamily: 6,
        DeviceFamilyQualification: 0,
        Name: "test",
        Description: "",
        Random: "2018-08-29T15:16:47",
        IsPublicAPI: true)

sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
sampler.setPostBodyRaw(true);               

更多信息: