json嵌套前后台解析及jsp中EL获取【我】
后台java解析
package com.test; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSONObject; public class Json1 { public static void main(String[] args) { // String reqJson = _request.getParameter("reqJson"); String reqJson = "{\"idExpDate\":\"2099-12-31\",\"partyName\":\"华夏银行股份有限公司\",\"partyNumber\":\"BJ2018122110041065\",\"identityType\":\"2\",\"custTree\":{\"custTreeTopNodeCode\":\"100207\",\"controlArea\":\"\",\"controlAreaTop\":\"\",\"custTreeTopName\":\"华夏银行股份有限公司\",\"custTreeNodeCode\":\"100207\",\"controlDepTop\":\"13007005\",\"controlTypeTop\":\"13013001\",\"controlType\":\"13013001\",\"custTreeName\":\"华夏银行股份有限公司\",\"controlDep\":\"13007005\"},\"orgUscc\":\"9111000010112001XW\",\"partyCert\":[{\"certNum\":\"qazwsxed3\",\"certType\":\"\",\"partyCertId\":\"259941363\"},{\"certNum\":\"9111000010112001XW\",\"certType\":\"49\",\"partyCertId\":\"820108430\"}],\"industryCd\":\"DD0302\",\"isAvailable\":\"1\",\"idEffDate\":\"1992-10-14\",\"industryTypeId\":\"177\",\"orgStatusCd\":\"4\",\"partyId\":\"259941363\",\"registerAddr\":\"北京市东城区建国门内大街22号\"}"; JSONObject jsonData = JSONObject.parseObject(reqJson); String partyName = String.valueOf(jsonData.get("partyName")); List<Map<String, String>> partyCertList = (List<Map<String, String>>) jsonData.get("partyCert"); for (int i = 0; i < partyCertList.size(); i++) { Map partyCertMap = partyCertList.get(i); System.out.println(partyCertMap); } } } /* { "idExpDate": "2099-12-31", "partyName": "华夏银行股份有限公司", "partyNumber": "BJ2018122110041065", "identityType": "2", "custTree": { "custTreeTopNodeCode": "100207", "controlArea": "", "controlAreaTop": "", "custTreeTopName": "华夏银行股份有限公司", "custTreeNodeCode": "100207", "controlDepTop": "13007005", "controlTypeTop": "13013001", "controlType": "13013001", "custTreeName": "华夏银行股份有限公司", "controlDep": "13007005" }, "orgUscc": "9111000010112001XW", "partyCert": [ { "certNum": "qazwsxed3", "certType": "", "partyCertId": "259941363" }, { "certNum": "9111000010112001XW", "certType": "49", "partyCertId": "820108430" } ], "industryCd": "DD0302", "isAvailable": "1", "idEffDate": "1992-10-14", "industryTypeId": "177", "orgStatusCd": "4", "partyId": "259941363", "registerAddr": "北京市东城区建国门内大街22号" } */
前台jsp获取:
<script type="text/javascript"> var regionType='${sessionScope.SSO_AUTH_USER.regionType}'; var handleOrgId='${sessionScope.SSO_AUTH_USER.orgId}'; var systemUserId = '${sessionScope.SSO_AUTH_USER.acctId}'; var staffId = '${sessionScope.SSO_AUTH_USER.staff_id}'; var regionCode = '${sessionScope.SSO_AUTH_USER.regionCode}'; //跳转页面时的请求json,内涵party信息 var reqJson = ${map.reqJson}; </script>
注意:如果map.reqJson的值是一个json串,那么在EL表达式这里不要加 双/单 引号。
前台js解析:
$(function() { //任意获取一组有效的partyCert,并去掉多余属性 var pratyCertArr = reqJson.partyCert; var certNum = ""; var certType = ""; $.each(pratyCertArr, function(i, item) { if(item.certNum && item.certType){ certNum = item.certNum; certType = item.certType; return false; } }); delete reqJson["partyCert"]; reqJson.certNum = certNum; reqJson.certType = certType; delete reqJson["custTree"]; // alert(JSON.stringify(reqJson)); init(); });