使用Groovy进行JSON输出 [英] JSON output with Groovy
问题描述
我一直在尝试使用groovy Jsonbuilder,正如你在下面看到的,试图用不同的方式来构建JSON对象和数组。事情开始有意义之后,我尝试扩展到下面所示的内容。我的问题是,为什么content出现在json漂亮的字符串输出中?我实际上有另一个json对象在json字符串输出中显示this.class信息。
任何想法?
def tt = [test,test1我试过这个,所以它肯定是一个明显的例子。 ]
def jjj =jason
def js3 = new groovy.json.JsonBuilder()
def js2 = new groovy.json.JsonBuilder(tt);
js3 hello:$ jjj,$ jjj:tt
def js4 = new groovy.json.JsonBuilder()
def result = js4([sdn:js3,openflow:js2 ,键入:3])
println js4.toPrettyString();
$ b {
sdn:{
content:{
hello:jason,
jason :[
test,
test1
]
}
},
openflow:{
content: [
test,
test1
]
},
type:3
}
问题可以重申为...
为什么会这样:
import groovy.json。*
def js3 = new JsonBuilder([test,test1])
def js4 = new JsonBuilder(js3)
println js4.toString()
print:
{content:[test,test1]}
和
import groovy.json。*
def js3 = new JsonBuilder([test,test1] )
def js4 = new JsonBuilder(js3.content)
println js4.toString()
打印此(?) :
[test,test1]
简短的回答是,JsonBuilder有一个名为 content
的成员,它代表有效负载。当一个JsonBuilder吸收另一个时,我们想要替换有效载荷,而不是嵌套它。这条线是替换有效载荷的方式:
def js4 = new JsonBuilder(js3.content)
最终,这源于以下事实: JsonBuilder.toString()
(代码在这里) JsonOutput.toJson(object)
此处的代码) 。
读者的练习是试验:
class MyBuilder {
def content
}
def myB = new MyBuilder(content:[test,test1])
println JsonOutput.toJson(myB )
println JsonOutput.toJson(myB.content)
I have been experimenting with the groovy Jsonbuilder as you can see below trying to look at different ways to build JSON objects and arrays. After things started to make sense, I tried expanding to what is shown below. The question I have is, why does "content" show up in the json pretty string output? I actually have another json object displaying this.class information in json string outputs.
Any ideas? I'm new to this, so it could definitely be an obvious one.
def tt = ["test", "test1"]
def jjj = "jason"
def js3 = new groovy.json.JsonBuilder()
def js2 = new groovy.json.JsonBuilder(tt);
js3 hello: "$jjj", "$jjj": tt
def js4 = new groovy.json.JsonBuilder()
def result = js4([sdn: js3, openflow: js2, type: 3])
println js4.toPrettyString();
{
"sdn": {
"content": {
"hello": "jason",
"jason": [
"test",
"test1"
]
}
},
"openflow": {
"content": [
"test",
"test1"
]
},
"type": 3
}
The problem can be restated as...
why does this:
import groovy.json.*
def js3 = new JsonBuilder(["test", "test1"])
def js4 = new JsonBuilder(js3)
println js4.toString()
print:
{"content":["test","test1"]}
and this:
import groovy.json.*
def js3 = new JsonBuilder(["test", "test1"])
def js4 = new JsonBuilder(js3.content)
println js4.toString()
prints this (?) :
["test","test1"]
The short answer is that JsonBuilder has a member named content
, which represents the payload. When one JsonBuilder absorbs another, we want to replace the payload, and not nest it. This line is the way to replace the payload:
def js4 = new JsonBuilder(js3.content)
Ultimately, this stems from the fact that JsonBuilder.toString()
(code here) calls JsonOutput.toJson(object)
(code here).
An exercise for the reader is to experiment with:
class MyBuilder {
def content
}
def myB = new MyBuilder(content: ["test", "test1"])
println JsonOutput.toJson(myB)
println JsonOutput.toJson(myB.content)
这篇关于使用Groovy进行JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!