-1

我必须使用 groovy 创建一个 json 有效负载,就像 -

{
  "source":1,
  "target":0 
}

源值和目标值将从 0 开始动态变化。你能帮我解决这个问题吗?

4

2 回答 2

0

通常很好用JsonOutput。您可以从地图构建 json:

def map = [source:1, target:0]
def out = new groovy.json.JsonOutput()
println out.prettyPrint(out.toJson(map))
于 2018-01-10T08:05:10.970 回答
0

您可以使用JsonBuilder创建所需的 json,如下所示。

//Define value or assign it dynamically for target and source as shown below
def tValue = 0
def sValue = 1

def json = new groovy.json.JsonBuilder()
json {
  source sValue
  target tValue
}
println json.toPrettyString()

您可以在线快速试用demo

于 2018-01-10T07:47:19.267 回答