[英]How to convert JSON value to escaped String in groovy [JsonSlurper]?
i have a groovy script that runs on Jenkins, i have build there a json object using JsonSlurper我有一个在 Jenkins 上运行的 groovy 脚本,我在那里构建了一个 json ZA8CFDE6331BD59EB2AC66F8911C46
The json object is a nested one, i would need to convert the nested json child into escaped string value instead of a json object (that's the requirement:) ). The json object is a nested one, i would need to convert the nested json child into escaped string value instead of a json object (that's the requirement:) ).
{"key1":
{"key2":
{"key3":true}
}
}
Into string escaped value:进入字符串转义值:
{"key1": " {\"key2\":{\"key3\":true}} " }
I'm building the json object by using:我正在使用以下方法构建 json object:
def jsont = new JsonSlurper().parseText(row)
doing some manipulation to the json, then need to convert to string:对 json 进行一些操作,然后需要转换为字符串:
jsont.key1 = func(jsont.key1) ----> here i want to convert key1 value to escaped
string jsont.key1 = func(jsont.key1) ----> here i want to convert key1 value to escaped
字符串
Any suggestion?有什么建议吗?
import groovy.json.*
def json = '''{"key1":
{"key2":
{"key3":true}
}
}
'''
def obj = new JsonSlurper().parseText(json)
obj.key1 = JsonOutput.toJson(obj.key1)
json = JsonOutput.toJson(obj)
result:结果:
{"key1":"{\"key2\":{\"key3\":true}}"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.