2017-05-29 6 views
0

JSONをレスポンスとして取得しているサーバー上で利用可能なドキュメントを取得するために、安らかなサービスを呼び出しています。今JSON Stringをビルドするときに2つのループを繰り返します。

{ 
    "results": [ 
     { 
      "result": { 
       "name": "Test traisy", 
       "version": "sdvdsv", 
       "author": "sdvdsv" 
      } 
     }, 
     { 
      "result": { 
       "name": "Jaspersoft Ultimate guide", 
       "version": "sdfdsv", 
       "author": "sdvdsv" 
      } 
     }, 
     { 
      "result": { 
       "name": "Dohrn", 
       "version": "12.19.00", 
       "author": "sdfdsf" 
      } 
     } 

    ] 
} 

コード

String accept = getValue("Accept"); 
accept = "application/xml";     
if ("application/xml".equals(accept)){ 
    builder=new groovy.xml.MarkupBuilder(writer); 
}else{ 
    builder=new groovy.json.JsonBuilder(); 
} 

    builder{ 
    results foaList.collect{ 
     [ 
        //Here I want to loop through the otaList to do something like that "ota.getName(), foa.getFlexiObject().getByString(ota.getName())" 
      result: [ 
        name: it.getFlexiObject().getByString("name"), 
       version: it.getFlexiObject().getByString("version"), 
       author: it.getFlexiObject().getByString("author") 
       ] 
     ] 
    } 

} 

:このリンク

http://localhost:8080/httpConnector/Rest/Documents?Accept=application/json

私は下にJSON文字列を取得していますを呼び出すときに、私はそうJSONBuilderでJSON文字列を構築しています私はプログラムでプロパティを追加したい。したがって、私はそのようなことをするためにotaListをループする必要があります

builder.'results'() { 
    for(FlexiObjectAttachment foa: foaList){ 
     for(ObjectTypeAttribute ota : otaList){ 
      param.put(ota.getName(), foa.getFlexiObject().getByString(ota.getName())); 

     } 
     result(param); 
    }  
} 

このバージョンはxml resposeのためだけに機能します。

答えて

0

あなたが試すことができるのは、collectコールでfoaotaの組み合わせを直接行うことです。 あなたの最初に作成されたdictは、正しい構造を持っています。

def foaList = [1, 2, 3, 4] 
def otaList = ['A', 'B', 'C'] 

foaList.collect { foa -> 
    result = [name: "Name$foa", version: "v$foa", author: "Author$foa"] 
    otaList.each { ota -> result[ota] = "$ota$foa" } 
    [ result: result ] 
} 
以下の例のような何かに
関連する問題