0

我在数组中有以下 JSON,当我尝试为此 JSON 设置值时,脚本通过但未设置值:

{
    "langauageCode": "en-US",
    "Test": [{
        "_modificationTypeCode": "added",
        "allocationTypeCode": "3",
        "code": "Test1"
    }]
}

我的代码:

def jsonRequest = slurper.parseText(rawRequest)
def builder = new JsonBuilder(jsonRequest)
builder.content.Test.code[0] ='Test2' //Code value is not getting set to 'Test2'
log.info  builder.toPrettyString()

我没有正确设置值吗?

4

1 回答 1

2

我假设这slurper是一个实例JsonSlurper。如果是这样,则根本不需要使用JsonBuilder,因为sluper返回 a 的实例Map。所以:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def req = '''{
    "langauageCode": "en-US",
    "Test": [{
        "_modificationTypeCode": "added",
        "allocationTypeCode": "3",
        "code": "Test1"
    }]
}'''
def slurped = new JsonSlurper().parseText(req)
slurped.Test[0].code = 'Test2'

println JsonOutput.prettyPrint(JsonOutput.toJson(slurped))
于 2019-04-04T06:49:55.190 回答