我想在我的应用程序中使用 json builder
做这样的东西new JsonBuilder() {
persons.collect{
[
name: it.name,
age: it.age,
companies: [{
company1: it.company //The company comes from person object.
},
{
company2: it.company
}
]
]
}
}
在这里,在访问公司空指针时,由于它没有考虑人员迭代器,因此会抛出异常。有没有其他方法可以做到这一点??
您可以定义iterator
的名称(闭包默认参数名称 - it
(
new JsonBuilder() {
persons.collect{person->
[
name: person.name,
age: person.age,
companies: [
{
company1: person.company //The company comes from person object.
},
{
company2: person.company
}
]
]
}
}