0

我想在我的应用程序中使用 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
                                    }
                        ]
                ]
            }
        }

这里在访问公司空指针时抛出异常,因为它没有考虑人员迭代器。有没有其他方法可以这样做?

4

1 回答 1

0

可以定义名称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
                }
            ]
        ]
    }
}
于 2017-06-12T11:32:22.987 回答