如何在 Groovy 的反斜杠中嵌入值时更新 JSON 负载
How to update JSON payload when values are embedded in backslash in Groovy
在 Groovy 中,我必须更新 JSON 负载中的值并进行 API 调用。我 运行 在更新有效负载时面临挑战,因为字段嵌入反斜杠中。是否有更简单的方法直接更新以下有效负载中的服务器,即更新 1. JSON 有效载荷 到 2。已更新 JSON 有效载荷(更新名称和主机值)。
1. JSON 负载:
{
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
2。已更新 JSON 负载:
{
"environment": "dev",
"config": "Update",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_2\",\"host\":\"test123.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
尝试了以下(在转换过程中丢失反斜杠):
代码:
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
jsonResp.Servers.name = "Server-test_2"
jsonResp.Servers.host = "test123.com"
Servers
是你初始 json 中的一个字符串 - 你必须解析它
import groovy.json.*
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
def servers = parser.parseText(jsonResp.Servers)
servers[0].name="Server-test_2"
servers[0].host="test123.com"
jsonResp.Servers = JsonOutput.toJson(servers)
json = JsonOutput.prettyPrint(JsonOutput.toJson(jsonResp))
在 Groovy 中,我必须更新 JSON 负载中的值并进行 API 调用。我 运行 在更新有效负载时面临挑战,因为字段嵌入反斜杠中。是否有更简单的方法直接更新以下有效负载中的服务器,即更新 1. JSON 有效载荷 到 2。已更新 JSON 有效载荷(更新名称和主机值)。
1. JSON 负载:
{
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
2。已更新 JSON 负载:
{
"environment": "dev",
"config": "Update",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_2\",\"host\":\"test123.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
尝试了以下(在转换过程中丢失反斜杠):
代码:
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[\{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
jsonResp.Servers.name = "Server-test_2"
jsonResp.Servers.host = "test123.com"
Servers
是你初始 json 中的一个字符串 - 你必须解析它
import groovy.json.*
def json = $/ {
"environment": "dev",
"config": "Create",
"configType": "Server",
"ServerName": "",
"Servers": "[{\"name\":\"Server-test_1\",\"host\":\"test.com\",\"port\":\"443\",\"tls\":\"2-way\"}]",
"tsHost": "",
"tsPort": "",
"tsSSLOption": "1-way"
}
/$
def parser = new JsonSlurper()
def jsonResp = parser.parseText(json)
println(jsonResp.Servers)
def servers = parser.parseText(jsonResp.Servers)
servers[0].name="Server-test_2"
servers[0].host="test123.com"
jsonResp.Servers = JsonOutput.toJson(servers)
json = JsonOutput.prettyPrint(JsonOutput.toJson(jsonResp))