My Groovy is 2.4.0
My code:
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)
The results are as below:
{
"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
私の質問は次のとおりです。
- builder2.content を println すると、content99 のコンテンツが表示されないのはなぜですか (クラスの何かが表示されるだけです)。
builder2.content99 を println すると、Groovy は次のように表示します。
groovy.lang.MissingPropertyException: そのようなプロパティはありません: クラスの content99: groovy.json.JsonBuilder
builder2.book を印刷しようとしても、Groovy は同じエラーを表示します。
groovy.lang.MissingPropertyException: そのようなプロパティはありません: クラスの本: groovy.json.JsonBuilder
Json オブジェクトのプロパティを読み取るにはどうすればよいですか?
ありがとうございました。