我的 Groovy 是 2.4.0
我的代码:
def builder2 = new JsonBuilder()
builder2.book {
isbn '0321774094'
title 'Scala for the Impatient'
author (['Cay S. Horstmann', 'Hellen'])
publisher 'Addison-Wesley Professional'
content99 {
contentType '1'
text 'Hello'
}
}
println(builder2.toPrettyString())
println(builder2.content)
println(builder2.content99)
println(builder2.book)
结果如下:
{
"book": {
"isbn": "0321774094",
"title": "Scala for the Impatient",
"author": [
"Cay S. Horstmann",
"Hellen"
],
"publisher": "Addison-Wesley Professional",
"content99": {
"contentType": "1",
"text": "Hello"
}
}
}
[book:[isbn:0321774094, title:Scala for the Impatient, author:[Cay S. Horstmann, Hellen], publisher:Addison-Wesley Professional, content99:TestJson$_testJson_closure1$_closure2@38ee79e5]]
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: content99 for class: groovy.json.JsonBuilder
Groovy.lang.MissingPropertyException: No such property: book for class: groovy.json.JsonBuilder
我的问题是:
- 当我 println builder2.content 时,为什么 content99 的内容不显示(它只显示类的东西)?
当我 println builder2.content99 时,Groovy 告诉我:
groovy.lang.MissingPropertyException:没有这样的属性:content99 类:groovy.json.JsonBuilder
即使我尝试 println builder2.book,Groovy 仍然告诉我同样的错误:
groovy.lang.MissingPropertyException:没有这样的属性:类的书:groovy.json.JsonBuilder
如何读取 Json 对象中的属性?
谢谢你。