【问题标题】:Using json builder使用 json 构建器
【发布时间】:2017-06-12 10:47:56
【问题描述】:

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

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

【问题讨论】:

    标签: groovy jsonbuilder


    【解决方案1】:

    可以定义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
                    }
                ]
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2020-04-21
      • 2023-03-22
      • 2023-02-03
      • 2016-11-16
      • 2023-03-31
      • 2020-06-05
      相关资源
      最近更新 更多