在Groovy中生成JSON对象 [英] Generate JSON object in Groovy
本文介绍了在Groovy中生成JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,我无法使用JSONBuilder在Groovy中创建JSON对象
For some reason I am not able to create JSON object in Groovy using JSONBuilder
这里是我所拥有的,但又回来了{}:
Here is what I have but it comes back {}:
import groovy.json.JsonBuilder
JsonBuilder builder = new JsonBuilder()
builder {
name "Name"
description "Description"
type "schedule type"
schedule {
recurrenceType "one time"
start "${startDateTime}"
end "${endDateTime}"
}
scope {
entities ["${applicationId}"]
matches [
{
tags [
{
key "key name"
context "some context"
}
]
}
]
}
}
有人知道使用嵌套元素创建JSON对象的简单方法吗?
Does anyone know a simple way to create JSON object with nested elements?
推荐答案
我倾向于找到 JsonOutput
,使其更易于用于已构造的数据.您的样子如下:
I tend to find JsonOutput
to be simpler to use for data that is already constructed. Yours would look like this:
groovy.json.JsonOutput.toJson(
[name: "Name",
description: "Description",
type: "schedule type",
schedule: [
recurrenceType: "one time",
start: "${startDateTime}",
end: "${endDateTime}"
],
scope: [
entities: ["${applicationId}"],
matches: [
[
tags: [
[
key: "key name",
context: "some context"
]
]
]
]
]]
)
这篇关于在Groovy中生成JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文