创建的JSON对象有两个附加属性:
contentHash
originalClassName
它们会自动添加,我不想要。 PFB代码
class Info{
def summary
def description
}
class Simple{
def start
def finish
def status
}
def buildJson(def info, def simple)
{
def jsonBuilder = new groovy.json.JsonBuilder()
jsonBuilder(info: info, simple: simple)
jsonBuilder.toPrettyString()
}
Json从上面的代码创建 -
{
"info": {
"contentHash": "a36cfa5d54ea40c843fff70e3e6e788e",
"originalClassName": "Info",
"summary":"Summary",
"description": "Description"
},
"simple": [
{
"contentHash": "1aab6dd693268f65224940a03a51c25b",
"start": "2017-09-10T08:54:05+0000",
"originalClassName": "ExampleTest",
"status": "PASS",
"finish": "2017-09-10T08:54:16+0000"
},
{
"contentHash": "1aab6dd693268f65224940a03a51c25b",
"start": "2017-09-10T08:53:37+0000",
"originalClassName": "ExampleTest",
"status": "PASS",
"finish": "2017-09-10T08:54:01+0000"
}
]
}
我不想要这两个属性,我不确定为什么它会被添加到第一位。有没有办法直接生成精确的JSONObject
。
答案 0 :(得分:0)
我在升级到groovy 2.4.12时遇到了这个问题。我通过定义对象类来解决这个问题
您可以尝试
def info = new Object()
info.metaClass.summary = "Info"
info.metaClass.description = "Description"
def simple = new Object()
simple.metaClass.start = "start"
simple.metaClass.finish = "finish"
simple.metaClass.status = "status"
def buildJson(def info, def simple)
{
def jsonBuilder = new groovy.json.JsonBuilder()
jsonBuilder(info: info, simple: simple)
println jsonBuilder.toPrettyString()
}
这将不包括元属性“ originalClassName”和“ contentHash”