先看看源代码:
_page=voucher4TestCSharp.queryForPagePDF(admdivcode,styeay, vtcode,star_voucherno,curPage, pageSize); if(_page!=null){ list = _page.getResultList(); } listMap = new ArrayList<Map<String,String>>(); for(VoucherDTO_PDF newVoucherDTO_PDF:list){ Map<String,String> map = null; map = BeanUtils.describe(newVoucherDTO_PDF); String voucher_no=newVoucherDTO_PDF.getVoucher_no(); String quhua_code=newVoucherDTO_PDF.getAdmdivcode(); String vt_code=newVoucherDTO_PDF.getVt_code(); String styear=newVoucherDTO_PDF.getStyear(); map.put("download", "<a href=javascript:download('"+voucher_no+"','"+quhua_code+"','"+vt_code+"','"+styear+"')>下凭证载</a>"); listMap.add(map); } Map<String,List<Map<String,String>>> resultMap = new HashMap<String,List<Map<String,String>>>(); resultMap.put("data", listMap); JSONObject jo = JSONObject.fromObject(resultMap); jo.put("recordCount", _page.getTotalRows()); this.actionWrite(jo.toString()); return null;
写回到前台的数据JO:
{"data":[ {"admdivcode":"110000","voucher_no":"TCNO1818745456","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO1818745456','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCNO174881120","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO174881120','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCNO1981697224","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO1981697224','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCNO2054239848","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO2054239848','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"yk00002","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('yk00002','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCNO323306459","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO323306459','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"LL00001","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('LL00001','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"yk00001","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('yk00001','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCNO218019412","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCNO218019412','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCN323306459","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCN323306459','110000','1101','2014')>下凭证载<\/a>"}, {"admdivcode":"110000","voucher_no":"TCN11O323306459","vt_code":"1101","styear":"2014","download":"<a href=javascript:download('TCN11O323306459','110000','1101','2014')>下凭证载<\/a>"} ], "recordCount":11}
Map<String,String> map = null;map = BeanUtils.describe(newVoucherDTO_PDF);
Listt数组,再将List转换成Map数组,
List<Map<String,String>> listMap=null; listMap = new ArrayList<Map<String,String>>(); listMap.add(map);
Map<String,List<Map<String,String>>> resultMap = new HashMap<String,List<Map<String,String>>>(); 放入数据: resultMap.put("data", listMap);
将map转换为JSON数据: JSONObject jo = JSONObject.fromObject(resultMap);
对于前台的数据接收:前台其实是自动将JSON解析出来的,不需要程序员的参与,JSON本身就是一个Map,一个标准。【以下是公司封装的,只供参考】
一个关于得到GridPanel内容的函数:
function getStore(urlpath, fileds) { var fields = getFiled(fileds); var store = new Ext.data.JsonStore( { proxy: { type: 'ajax', url : urlpath, timeout: 90000, reader: { <strong>type: 'json', root: 'data', totalProperty : 'recordCount'</strong> } }, pageSize: itemsPerPage, //remoteSort : true, fields : fields, autoLoad : false }); return store; }
再看看var fields = getFiled(fileds);此时功能只是要显示部分列选项数据。
function getFiled(filed) { var fields = filed.split(","); var filed = "["; for ( var i = 0; i < fields.length; i++) { filed = filed + "{name: '" + fields[i] + "'},"; } filed = filed.substring(0, filed.length - 1); filed = filed + "]"; return strToJson(filed); }
可以看到这个fields的格式其实和data根数据的格式是相对应的。
总结:不管怎样后台传递JSON数据的格式是固定的,这点要清楚,这是一个规范。
JSON基础:http://baike.baidu.com/view/136475.htm#【百度百科】
以上是自己对后台拼JSON到前台的领悟,非常不清楚,只供参考。