Groovy JSONBuilder问题 [英] Groovy JSONBuilder issues
问题描述
我试图在Groovy中使用JsonBuilder来动态生成JSON。我想创建一个JSON块,如下所示:
{
type:{
__type :urn,
value:myCustomValue1
},
urn:{
__type:urn,
value :myCustomValue2
},
date:{
epoch:1265662800000,
str:2010-02-08T21:00:00Z
$ bmetadata:[{
ratings:[{
rating:NR,
scheme:eirin,
_type:{
__type:urn,
value:myCustomValue3
}
}],
creators: [Jim,Bob,Joe]
}]
}
我已经书面:
def addUrn(parent,type,urnVal){
parent。$ type{
__typeurn
valueurnVal
}
}
字符串getEpisode(字符串myCustomVal1,字符串myCustomVal2,字符串myCustom Val3){
def builder = new groovy.json.JsonBuilder()
def root = builder {
addUrn(builder,type,myCustomVal1)
addUrn(builder, urn,some:urn:$ myCustomVal2)
date{
epoch 1265662800000
str2010-02-08T21:00:00Z
}
metadata({
ratings({
ratingG
schemeeirin
addUrn(builder,_type,$ myCustomVal3)
})
创建者Jim,Bob,Joe
})
}
return root.toString();
}
但我遇到以下问题:
- 每当我调用
addUrn
时,字符串中都不会返回任何内容。 我是否误解如何在Groovy中使用方法? - 没有任何值在返回的字符串中封装为双引号(或单引号)。
- 无论何时我使用
{
,我都会在返回的值中获得'_getEpisode_closure2_closure2 @(insert hex)'。
我的语法有什么问题吗?或者有人可以指点一些使用超出简单值的方法和/或示例的示例/教程(例如,数组中的嵌套值)。
注意:这是一个淡化的例子,但我试图保持在给我的问题领域的复杂性。
您必须在 addUrn
方法中使用委托
,而不是
传递你正在工作的 builder
。
root
而不是 builder
。 解决是否遵循#2。
def builder = new groovy.json.JsonBuilder()
def root = builder {
name 代文
数据{
类型测试
注虚拟
}
addUrn(代表,性别,男性)
addUrn(委托,zip,43230)
}
def addUrn(父类型,urnVal){
父类型。$ type{
__typeurn
valueurnVal
}
}
println builder.toPrettyString()
输出: -
name:Devin,
data:{
type:Test,
注:虚拟
},
性别:{
__type:urn,
值:男性
} ,
zip:{
__type:urn,
value:43230
}
}
I'm trying to use JsonBuilder with Groovy to dynamically generate JSON. I want to create a JSON block like:
{
"type": {
"__type": "urn",
"value": "myCustomValue1"
},
"urn": {
"__type": "urn",
"value": "myCustomValue2"
},
"date": {
"epoch": 1265662800000,
"str": "2010-02-08T21:00:00Z"
},
"metadata": [{
"ratings": [{
"rating": "NR",
"scheme": "eirin",
"_type": {
"__type": "urn",
"value": "myCustomValue3"
}
}],
"creators": [Jim, Bob, Joe]
}]
}
I've written:
def addUrn(parent, type, urnVal) {
parent."$type" {
__type "urn"
"value" urnVal
}
}
String getEpisode(String myCustomVal1, String myCustomVal2, String myCustomVal3) {
def builder = new groovy.json.JsonBuilder()
def root = builder {
addUrn(builder, "type", myCustomVal1)
addUrn(builder, "urn", "some:urn:$myCustomVal2")
"date" {
epoch 1265662800000
str "2010-02-08T21:00:00Z"
}
"metadata" ({
ratings ({
rating "G"
scheme "eirin"
addUrn(builder, "_type", "$myCustomVal3")
})
creators "Jim", "Bob", "Joe"
})
}
return root.toString();
}
But I've run into the following issues:
- Whenever I call
addUrn
, nothing is returned in the string. Am I misunderstanding how to use methods in Groovy? - None of the values are encapsulated in double (or single) quotes in the returned string.
- Anytime I use a
{
, I get a '_getEpisode_closure2_closure2@(insert hex)' in the returned value.
Is there something wrong with my syntax? Or can someone point me to some example/tutorial that uses methods and/or examples beyond simple values (e.g. nested values within arrays).
NOTE: This is a watered down example, but I tried to maintain the complexity around the areas that were giving me issues.
You have to use
delegate
inaddUrn
method instead of passing thebuilder
on which you are working.It is because you are doing a
toSting()
ortoPrettyString()
onroot
instead ofbuilder
.Solved if #2 is followed.
Sample:
def builder = new groovy.json.JsonBuilder()
def root = builder {
name "Devin"
data {
type "Test"
note "Dummy"
}
addUrn(delegate, "gender", "male")
addUrn(delegate, "zip", "43230")
}
def addUrn(parent, type, urnVal) {
parent."$type" {
__type "urn"
"value" urnVal
}
}
println builder.toPrettyString()
Output:-
{
"name": "Devin",
"data": {
"type": "Test",
"note": "Dummy"
},
"gender": {
"__type": "urn",
"value": "male"
},
"zip": {
"__type": "urn",
"value": "43230"
}
}
这篇关于Groovy JSONBuilder问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!