原始报文: 请求报文: [ { "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