2017-05-29 52 views
0

我打电话给一个restful服务以将数据库中的文档作为JSON字符串。所以,我有两个循环foaList, otaList当我做下面还工作得
一个循环,我得到正确的JSON响应:构建Json数组时,只有最后一个对象出现在数组中

此代码获取正确的回应:

def param = new HashMap(); 
    builder{ 
     results foaList.collect{ 
      [ 
       result: [ 
             //Here I have not added all json keys and vlaues as in the second case. 
          name: it.getFlexiObject().getByString("name"), 
          version: it.getFlexiObject().getByString("version"), 
         author: it.getFlexiObject().getByString("author") 
       ] 
      ] 
     }  
    } 

JSON字符串与一个foaList循环:

{ 
    "results": [ 
     { 
      "result": { 
       "name": "Benzin consume", 
       "version": "09.55.00", 
       "author": "Alex" 
      } 
     }, 
     { 
      "result": { 
       "name": "Jaspersoft Ultimate guide", 
       "version": "sdfdsv", 
       "author": "sdvdsv" 
      } 
     } 
    ] 
} 
{ 
    "results": [ 
     { 
      "result": { 
       "name": "Benzin consume", 
       "version": "09.55.00", 
       "author": "Alex" 
      } 
     }, 
     { 
      "result": { 
       "name": "Jaspersoft Ultimate guide", 
       "version": "sdfdsv", 
       "author": "sdvdsv" 
      } 
     } 
    ] 
} 

但是当我尝试遍历这两个列表我得到JSON字符串与相同的对象。

{ 
    "results": [ 
     { 
      "content": "", 
      "author": "Alex", 
      "description": "Test Json", 
      "link": "21120", 
      "name": "Benzin consume", 
      "fileName": "", 
      "contentType": "", 
      "version": "09.55.00" 
     }, 
     { 
      "content": "", 
      "author": "Alex", 
      "description": "Test Json", 
      "link": "21120", 
      "name": "Benzin consume", 
      "fileName": "", 
      "contentType": "", 
      "version": "09.55.00" 
     } 
    ] 
} 

我怎样才能获得JSON字符串两个对象通过这两个列表foaList, otaList迭代时:

def param = new HashMap(); 
    builder{ 
     results foaList.collect{ 
      otaList.eachWithIndex{item, index -> 
       param.put(item.getName(), it.getFlexiObject().getByString(item.getName())); 
       } 
       result:param  
     }  
    } 

JSON字符串经过两个循环的循环后?

回答

0

的问题是,我的PARAM地图是第一循环之外:

def param = new HashMap(); 
    builder{ 
     results foaList.collect{ 
      def param = new HashMap(); 
      otaList.eachWithIndex{item, index -> 
       param.put(item.getName(), it.getFlexiObject().getByString(item.getName())); 
       } 
     } 

    } 

但它应该是:

builder{ 
    results foaList.collect{ 
     def param = new HashMap(); 
     otaList.eachWithIndex{item, index -> 
      param.put(item.getName(), it.getFlexiObject().getByString(item.getName())); 
      } 

    } 

} 
相关问题