【发布时间】:2022-01-22 20:32:00
【问题描述】:
我正在尝试读取 json 文件,编辑其中的某些部分,然后将其解析回 json 文件。目标是改变汇合页面的价值。我在 Jenkins 管道中使用 groovy 代码。这里是:
def changeValue(){
def json_map = readJSON file: '/tmp/updater.json'
def body_content = '{"storage":{"value":'
body_content += '"<h1>test</h1>"'
body_content += ',"representation":"storage"}}'
json_map.body = body_content
json_as_string = new JsonBuilder(json_map).toPrettyString().replaceAll("\\\\", "") // It also adds unneccesary escapes
print json_as_string
}
这是updater.json的内容:
{
"id":"redacted",
"type":"page",
"title":"redacted",
"space":{"key":"redacted"},
"body":{"storage":{"value":"<h1>wrong</h1>","representation":"storage"}},
"version":{
"number":6
}
}
这就是我得到的:
{
"id": "redacted",
"type": "page",
"title": "redacted",
"space": {
"key": "redacted"
},
"body": "{"storage":{"value":"<h1>test</h1>","representation":"storage"}}",
"version": {
"number": 6
}
}
如您所见,它在正文块周围添加了引号。我怎样才能摆脱它们?
【问题讨论】: