如何在数组内设置Json的值

时间:2019-04-04 00:11:02

标签: json groovy ready-api jsonslurper

我在数组中具有以下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()

我设置的值不正确吗?

1 个答案:

答案 0 :(得分:2)

我假设<div class="timeline descending"> <div class="event"> Event 1 </div> <div class="event"> Event 2 </div> <div class="event open"> Event 3 </div> <div class="event"> Event 4 </div> <div class="event"> Event 5 </div> <div class="event"> Event 6 </div> </div>slurper的实例。如果是这样,则根本不需要使用JsonSlurper,因为JsonBuilder返回sluper的实例。所以:

Map