原始報文: 請求報文: [ { "appNo" : "", "applicantCredentialCode" : "", "applicantCredentialNo" : "", "applicantName" : "", "benfList" : [ { "appNo" : "", "beneficiaryCredentialCode" : "", "beneficiaryCredentialNo" : "", "beneficiaryName" : "", "seqNo" : "" } ], "createUser" : "", "insList" : [ { "appNo" : "", "insurantAmount" : "", "insurantCredentialCode" : "", "insurantCredentialNo" : "", "insurantName" : "", "seqNo" : "" } ], "insureAddress" : "", "insureWay" : "", "messge" : "", "policyEndDate" : "", "policyStartDate" : "", "policyState" : "", "policyStateDesc" : "", "policyTotalAmount" : "", "policyType" : "", "riskList" : [ { "appNo" : "", "insuranceTypeCode" : "", "insuranceTypeName" : "", "seqNo" : "" } ], "saveState" : "", "seqNo" : "", "uploadType" : "" }, { "appNo" : "", "applicantCredentialCode" : "", "applicantCredentialNo" : "", "applicantName" : "", "benfList" : [ { "appNo" : "", "beneficiaryCredentialCode" : "", "beneficiaryCredentialNo" : "", "beneficiaryName" : "", "seqNo" : "" } ], "createUser" : "", "insList" : [ { "appNo" : "", "insurantAmount" : "", "insurantCredentialCode" : "", "insurantCredentialNo" : "", "insurantName" : "", "seqNo" : "" } ], "insureAddress" : "", "insureWay" : "", "messge" : "", "policyEndDate" : "", "policyStartDate" : "", "policyState" : "", "policyStateDesc" : "", "policyTotalAmount" : "", "policyType" : "", "riskList" : [ { "appNo" : "", "insuranceTypeCode" : "", "insuranceTypeName" : "", "seqNo" : "" } ], "uploadType" : "" } ] 響應報文: { "respCode" : "0000", "respMsg" : "", "respData" : [{ "message" : "", "saveState" : "1", "seqNo" : "" }, { "message" : "", "saveState" : "1", "seqNo" : "" }] }
這是報文轉換的類:
1 package com.sinosoft.lis.bjbs.tasks.zhaoyongqiang; 2 3 import java.util.LinkedHashMap; 4 5 import javax.servlet.http.HttpUtils; 6 7 import com.alibaba.fastjson.JSONObject; 8 import com.sinosoft.serviceclient.bj.SCObjectCheatClient; 9 import com.sinosoft.serviceclient.bj.SCObjectCheatJsonClient; 10 import com.sinosoft.tech.log.LoggerFactory; 11 import com.sinosoft.tech.log.LoggerFactory; 12 import com.sinosoft.tech.log.Logger; 13 import com.sinosoft.tools.HttpUtil; 14 import com.sinosoft.tools.xmltools.framework.XmlConvertor; 15 import com.sinosoft.utility.CError; 16 import com.sinosoft.utility.CErrors; 17 import com.sinosoft.utility.ExeSQL; 18 import com.sinosoft.utility.SSRS; 19 import com.sinosoft.webservice.DTO.SCOC.SC_Global; 20 import com.sinosoft.webservice.DTO.SCOC.SC_Request; 21 import com.sinosoft.webservice.DTO.SCOC.SC_Requests; 22 23 public class SCJson { 24 static final Logger logger = LoggerFactory.getLogger(SCJson.class); 25 private CErrors mErrors = new CErrors(); 26 27 // 組裝json 這是前台傳過來的需要報送的結果集 28 private boolean submitData(SSRS tSSRS) { 29 if (tSSRS == null) { 30 buildError("submitData", "提取數據失敗!"); 31 return false; 32 } 33 if (tSSRS.getMaxRow() < 1) { 34 buildError("submitData", "沒有需要報送的數據!"); 35 return false; 36 } 37 int tRow = tSSRS.getMaxRow(); // 獲取行數 38 39 // 創建一個 40 JSONObject[] tRoots = new JSONObject[tRow]; 41 try { 42 for (int i = 1; i <= tRow; i++) { 43 // 為了保證json對象的順序,需要使用linkedhashmap對象對json中的數據進行控制 44 JSONObject tRoot = new JSONObject(new LinkedHashMap<String, Object>()); 45 tRoot.put("appNo", "");// 現在先將值賦值為空 46 tRoot.put("applicantCredentialCode", ""); 47 tRoot.put("applicantCredentialNo", ""); 48 tRoot.put("applicantName", ""); 49 50 // 根據報文的樣式 需要再次創建一個json數組 51 JSONObject[] benfList = assemblebenfList(""); 52 tRoot.put("benfList", benfList);// 注意:這里記得一定要放進去 53 tRoot.put("createUser", ""); 54 // 根據報文的樣式 需要再次創建一個json數組 55 JSONObject[] insList = assembleinsList(""); 56 tRoot.put("insList", insList); 57 tRoot.put("insureAddress", ""); 58 tRoot.put("insureWay", ""); 59 tRoot.put("messge", ""); 60 tRoot.put("policyEndDate", ""); 61 tRoot.put("policyStartDate", ""); 62 tRoot.put("policyState", ""); 63 tRoot.put("policyStateDesc", ""); 64 tRoot.put("policyTotalAmount", ""); 65 tRoot.put("policyType", ""); 66 67 JSONObject[] riskList = assembleriskList(""); 68 tRoot.put("riskList", riskList); 69 tRoot.put("saveState", ""); 70 tRoot.put("seqNo", ""); 71 tRoot.put("uploadType", ""); 72 tRoots[i - 1] = tRoot;// 將這個實體賦值到數組中 73 74 } 75 JSONObject tJSONObject = new JSONObject(); 76 tJSONObject.put("Test", tRoots); 77 String jsonReq = tJSONObject.toString();// 將json對象轉換成字符串 78 logger.info("請求json>>>>>>>>>>>" + jsonReq); 79 // TODO 調用http請求進行數據推送 80 // String returnJSON = 81 // HttpUtil.post("josn字符串","要請求的url");//這是請求返回的json字符串 82 String returnJson = "{\"respCode\":\"0000\",\"respMsg\":\"報送成功\",\"respData\":[{\"message\":\"\",\"saveState\":\"1\",\"seqNo\":\"\"},{\"message\":\"\",\"saveState\":\"1\",\"seqNo\":\"\"}]}"; 83 // 將字符串轉換成json 84 JSONObject parseObject = JSONObject.parseObject(returnJson); 85 // 獲取json某個節點的屬性值 86 87 String respCode = parseObject.getString("respCode"); 88 // 根據具體的需求對節點的值進行相應的判斷 89 if ("0000".equals(respCode)) { 90 logger.info("該批次推送成功!【" + parseObject.getString("respMsg") + "】"); 91 } else { 92 logger.info("該批次推送失敗!"); 93 } 94 } catch (Exception e) { 95 logger.info("該批次數據推送出現異常", e); 96 e.printStackTrace(); 97 98 } 99 100 return true; 101 } 102 103 private JSONObject[] assembleriskList(String string) { 104 // TODO 通過傳入條件查詢險種的信息 105 String tSql = "select '1','1','1','1','1','1' from dual ";// 是會使用到這個參數的 106 SSRS tSSRS = new ExeSQL().execSQL(tSql); 107 JSONObject[] tJSONObjects = new JSONObject[tSSRS.getMaxRow()];// 創建json對象 108 int size = tSSRS.getMaxRow(); 109 for (int i = 1; i <= size; i++) { 110 // 創建json對象 111 JSONObject tRoot = new JSONObject(); 112 tRoot.put("appNo", ""); 113 tRoot.put("insuranceTypeCode", ""); 114 tRoot.put("seqNo", ""); 115 tJSONObjects[i - 1] = tRoot; 116 } 117 return tJSONObjects; 118 } 119 120 private JSONObject[] assembleinsList(String string) { 121 // TODO 通過傳入條件查詢被保人的信息 122 String tSql = "select '1','1','1','1','1','1' from dual ";// 是會使用到這個參數的 123 SSRS tSSRS = new ExeSQL().execSQL(tSql); 124 JSONObject[] tJSONObjects = new JSONObject[tSSRS.getMaxRow()];// 創建json對象 125 int size = tSSRS.getMaxRow(); 126 for (int i = 1; i <= size; i++) { 127 // 創建json對象 128 JSONObject tRoot = new JSONObject(); 129 tRoot.put("appNo", ""); 130 tRoot.put("insurantAmount", ""); 131 tRoot.put("insurantCredentialCode", ""); 132 tRoot.put("insurantCredentialNo", ""); 133 tRoot.put("insurantName", ""); 134 tRoot.put("seqNo", ""); 135 tJSONObjects[i - 1] = tRoot; 136 } 137 return tJSONObjects; 138 } 139 140 /** 141 * 通過傳入的變量查詢受益人的信息 142 * 143 * @param variable 144 * @return 145 */ 146 private JSONObject[] assemblebenfList(String variable) { 147 // TODO 通過傳入條件查詢受益人信息 148 String tSql = "select '1','1','1','1','1','1' from dual ";// 是會使用到這個參數的 149 SSRS tSSRS = new ExeSQL().execSQL(tSql); 150 JSONObject[] tJSONObjects = new JSONObject[tSSRS.getMaxRow()];// 創建json對象 151 int size = tSSRS.getMaxRow(); 152 for (int i = 1; i <= size; i++) { 153 // 創建json對象 154 JSONObject tRoot = new JSONObject(); 155 tRoot.put("appNo", ""); 156 tRoot.put("beneficiaryCredentialCode", ""); 157 tRoot.put("beneficiaryCredentialNo", ""); 158 tRoot.put("beneficiaryName", ""); 159 tRoot.put("seqNo", ""); 160 tJSONObjects[i - 1] = tRoot; 161 } 162 return tJSONObjects; 163 } 164 165 /** 166 * 錯誤構造方法 167 * 168 * @param tFunctionName 169 * @param tErrorMessage 170 */ 171 private void buildError(String tFunctionName, String tErrorMessage) { 172 173 CError tCError = new CError(); 174 tCError.moduleName = this.getClass().getSimpleName(); 175 tCError.functionName = tFunctionName; 176 tCError.errorMessage = tErrorMessage; 177 mErrors.addOneError(tCError); 178 logger.error(tErrorMessage); 179 System.out.println(tErrorMessage); 180 } 181 182 public static void main(String[] args) { 183 StringBuffer tStringBuffer = new StringBuffer(); 184 tStringBuffer.append("select 'Upload_AAA','ContNo&InsuredNo' from dual "); 185 tStringBuffer.append("union "); 186 tStringBuffer.append("select 'Upload_BBB','AppntNo&BnfNo' from dual "); 187 188 new SCJson().submitData(new ExeSQL().execSQL(tStringBuffer.toString())); 189 } 190 }
其實json格式的請求或者響應報文是不需要創建對應的實體類的。直接使用jsonObject類中的方法進行轉換就行。
json報文轉Java實體類,xml報文轉實體類。網址:http://www.ofmonkey.com/tools/entit