如何通过GRAILS构建如下所述的JSOn数组?

时间:2014-07-22 12:32:32

标签: arrays json grails

如何使用grails创建JSON数组,如下所示:

{"paymentRequestlist":[{"sourceAccountNo":"555555555555555","sourceBankCode":"GLBBNPKA","destinationBankCode":"GLBBNPKA","destinationBankAccountNo":"123456789111111","corporateName":"","amount":14000,"remarks":"","narrationOne":"fund transfer initiated by maker 1","narrationTwo":"","uniqueId":"ESW-COR:1405491430740"},{"sourceAccountNo":"555555555555555","sourceBankCode":"GLBBNPKA","destinationBankCode":"GLBBNPKA","destinationBankAccountNo":"123456789111111","corporateName":"","amount":13000,"remarks":"","narrationOne":"fund transfer initiated by maker 1","narrationTwo":"","uniqueId":"ESW-COR:1405491430740"}]}

到目前为止,我已尝试使用grails.converters.JSON构建一个数组,如下所示:

JSONArray postParamsArray = new JSONArray()

JSONObject postParams = new JSONObject();
            postParams.put('sourceAccountNo',fundTransfer.fromAccount.accountNumber);
            postParams.put('sourceBankCode',fundTransfer.fromAccount.bank.bankCode);
            postParams.put('destinationBankCode',fundTransfer.toAccount.bank.bankCode);
            postParams.put('destinationBankAccountNo',fundTransfer.toAccount.accountNumber);
            postParams.put('corporateName','');
            postParams.put('amount',fundTransfer.amount);
            postParams.put('remarks','');
            postParams.put('narrationOne',fundTransfer.note);
            postParams.put('narrationTwo','');
            postParams.put('uniqueId',fundTransfer.uniqueId);

 postParamsArray.add(postParams);

P.S。我对grails完全不熟悉!!任何建议都会有很大的帮助

2 个答案:

答案 0 :(得分:1)

您可以创建地图(更容易)并将其转换为JSON

简单(未经测试)的例子

def json = ['paymentRequestlist':[
   ['sourceAccountNo': valueForSourceAccountNo1,
    'sourceBankCode': valueForSourceBankCode1,
    ...
   ],
   ['sourceAccountNo': valueForSourceAccountNo2,
    'sourceBankCode': valueForSourceBankCode2,
    ...
   ]]
] as grails.converters.JSON

答案 1 :(得分:0)

使用以下代码填写您的代码:

JSONObject completeJson = new JSONObject()
completeJson.put("paymentRequestlist",postParamsArray)

println completeJson as JSON