简体   繁体   中英

Replacing a number variable in json using groovy

I have a json which has some numbers that I want to make dynamic and replace the value using groovy. Please let me know if there is a better way as I have already used toInteger to convert but not working. For ex { jobName:"", protocolName:"tcp", portNo:"" } I am able to replace the job-name which is string using replace in groovy but I am not able to replace the port number. The final json should look like below

{ jobName:"myjob", protocolName:"tcp", portNo:1112 }

Please let me know if there is a way to do that in groovy

Something like this:

String str = '{ "jobName":"", "protocolName":"tcp", "portNo":"" }'
def json = new JsonSlurper().parseText(str)
json.jobName = 'myJob'
json.portNo = 1112
println new JsonOutput().prettyPrint(new JsonOutput().toJson(json))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM