2017-06-12 34 views
0

我想这样的事情在我的应用程序使用JSON建设者使用JSON建设者

new JsonBuilder() { 
      persons.collect{ 
       [ 
         name: it.name, 
         age: it.age, 
         companies: [{ 
             company1: it.company //The company comes from person object. 
            }, 
            { 
             company2: it.company 
            } 
         ] 
       ] 
      } 
     } 

这里当访问该公司空指针异常被抛出,因为它没有考虑到人iterator.Is有任何其他方式这样做?

回答

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 
       } 
      ] 
     ] 
    } 
} 
+0

谢谢。有效 – xxx