构建 groovy json 并需要 groovy 构建器的示例
empData1.groovy
def empMap[] =
[1:2,
2:2,
3:[1:2,3:3,4:890,A:B], //Map inside Map
4:4,
6:7
]
empData2.groovy
def empMap1[] =
[91:21,
92:22,
93:[81:82,83:3,84:890,A:B], ////Map inside Map
94:4,
96:7
]
emp3.groovy -
- Q1:我如何为 empMap/empMap1 构建一个构建器 - Q2:如果我想在 emp3.grrovy 中做类似的事情 empData1.include(empMap1) 将映射数据复制到map2
是相同的事情,如何通过groovy json实现?
这可以做
def json = new JsonBuilder()
def result = json {
1 2 //build a map with 1 as key 2 value without sinlge quaote is this possible
3 33 //build a map with 3 as key 33 value without sinlge quaote is this possible
}
println 结果
**Answer
def json = new JsonBuilder()
def result = json {
'1' '2'
'3' '33'
}
println result
我的输出 [1:2, 3:33]
但是我尝试构建这样的东西
def json = new JsonBuilder()
def result = json {
'1' '2'
'3' '33'
'4' (
'1' '3'
'4' '5'
)
}
println result
它给了我编译错误任何线索来解决它
empData1.groovy
def empMap[] =
[1:2,
2:2,
3:[1:2,3:3,4:890,A:B], //Map inside Map
4:4,
6:7
]
empData2.groovy
def empMap1[] =
[91:21,
92:22,
93:[81:82,83:3,84:890,A:B], ////Map inside Map
94:4,
96:7
]
emp3.groovy
-
- Q1: how can i build a builder for like empMap/empMap1
- Q2: if i want to do in emp3.grrovy like
empData1.include(empMap1) map data copies to map2
is the same thing is achievavle vis groovy json how ?
is this possible to do
def json = new JsonBuilder()
def result = json {
1 2 //build a map with 1 as key 2 value without sinlge quaote is this possible
3 33 //build a map with 3 as key 33 value without sinlge quaote is this possible
}
println result
**Answer
def json = new JsonBuilder()
def result = json {
'1' '2'
'3' '33'
}
println result
My output [1:2, 3:33]
but i try to build something like this
def json = new JsonBuilder()
def result = json {
'1' '2'
'3' '33'
'4' (
'1' '3'
'4' '5'
)
}
println result
it gives me compilation error any clue to resolve it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Anish,我建议你尝试这个来使其编译:
Anish, I suggest you try this instead to make it compile: