项目场景:
部分业务会涉及到其它服务器,会将自己数据上传到该服务器,然后根据上传的数据获取反馈的数据进行数据渲染,这个时候数据也许是根据json返回来的,所以解析数据需要费心一点
问题描述
在这我说说我遇到的问题,我是将自己的数据上传到了,webservice服务器,并从该服务返回的数据进行解析,发现只渲染了部分数据,当我要来日志之后才发现,返回的json是一个对象包裹着一个json数组
String result ="{\"output\":{\"epcinfo\":{\"epc_id\":\"20220705600162\",\"psn_no\":\"\",\"psn_cert_type\":\"01\",\"certno\":\"\",\"insutype\":\"\",\"org_med_type\":\"11\",\"org_mdtrt_id\":\"345106109\",\"dscg_main_dise_codg\":\" L03.003 \",\"dscg_main_dise_name\":\"甲沟炎\",\"dise_no\":\"\",\"dise_name\":\"\",\"dept_codg\":\"207\",\"dept_name\":\"胸外科\",\"begin_time\":\"2022-07-05 10:27:44\",\"end_time\":\"2022-07-06 00:00:00\",\"chfpdr_code\":\"0196\",\"crter_chfpdr_name\":\"\",\"crter_chfpdr_certno\":null,\"epc_sort\":\"2\",\"epc_type\":\"1\",\"medrcdno\":\"\",\"diag_dscr\":\"\",\"main_cond_dscr\":\"\",\"tcmdrug_used_way\":\"\",\"tcmdrug_cnt\":null,\"review_name\":\"\",\"adjust_name\":\"\",\"check_name\":null,\"expContent\":null,\"can_use_cnt\":1,\"use_time\":\"2022-07-05 10:27:44\",\"fixmedins_code\":\"H13052800362\",\"fixmedins_name\":\"\",\"use_fixmedins_code\":null,\"use_fixmedins_name\":null,\"mdtrt_id\":null,\"setl_id\":null,\"use_flag\":\"0\"},\"epclist\":[{\"regName\":\"环孢素软胶囊\",\"prodname\":\"环孢素软胶囊\",\"spec\":\"25mg*50\",\"sin_dos_dscr\":null,\"usedFrquDscr\":\"每日三次\",\"cnt\":39.0000,\"dosformName\":\"胶囊\",\"use_time\":null,\"mdtrt_id\":null,\"setl_id\":null,\"use_fixmedins_code\":null,\"use_fixmedins_name\":null,\"expContent\":null}]},\"infcode\":0,\"inf_refmsgid\":\"130499202207072236180012751570\",\"refmsg_time\":\"20220707223618\",\"respond_time\":\"20220707223618\",\"err_msg\":null,\"warn_msg\":null,\"signtype\":null,\"cainfo\":null}";
这里可能看的不直观我们解析一下,下面解析出来的图可以看到,他是有一个数组的
解析的话可以使用这个工具:
JSON转JAVA实体|在线JSON转JavaBean工具 - JSON.cn
https://www.json.cn/json/json2java.html
解决方案:
解决方案的话,就是需要把数组单独取出来,然后再塞进去可以参考以下代码
public JsonResult mdtrtinfoQuery(String psnNo, String epcId,String way) throws Exception {
JsonResult js = new JsonResult();
List<EpcSheet> data = new ArrayList<>();
List<Object> a = new ArrayList<Object>();
String param = psnNo + "|" + epcId+ "|";
String result = ClientUtil.callWebSVasmx("http://192.168.0.45:8082/WebServiceInsur.asmx", "run",way,param);
JSONObject epcsheets = JSON.parseObject(result);
Object output = epcsheets.get("output");
JSONObject outputs = JSON.parseObject(output.toString());
JSONObject epcinfo = outputs.getJSONObject("epcinfo");
EpcSheet epcSheet = JSON.parseObject(epcinfo.toString(),EpcSheet.class);
JSONArray jsonArray = outputs.getJSONArray("epclist");
//List<EpcDetail> list = JSON.parseArray(jsonArray.toString(),EpcDetail.class);
a.add(epcSheet);
a.add(jsonArray);
js.setData(a);
js.setCode("0");
return js;
}
这两句是取出上面那条sql的对象,括号里的是名字
JSONObject outputs = JSON.parseObject(output.toString());
JSONObject epcinfo = outputs.getJSONObject("epcinfo");
这两句是取出上面那条sql的数组,括号里的是名字
EpcSheet epcSheet = JSON.parseObject(epcinfo.toString(),EpcSheet.class);
JSONArray jsonArray = outputs.getJSONArray("epclist");
然后将数据添加进去然后返回给前端就可以了,至于前端接收,就要根据自己的实际情况来了,以下代码是我的接收方式
mdtrtinfoQuerys() {
let that = this;
//服务器后端地址
that.$axios.defaults.baseURL = 'http://localhost:xxxx';
that.$axios({
method: 'get',
url: '/xxxx?xxx=' + this.xxx.xxx + '&xxx=' + this.xxx.xxx + '&way=' +
that.way,
}).then((result) => {
console.log(result);
if (result.data.code == 0) {
// that.tableDataer = JSON.parse(JSON.stringify(result.data.data));
that.queryInfos = JSON.parse(JSON.stringify(result.data.data[0]));
that.baseForm.addForm = JSON.parse(JSON.stringify(result.data.data[1]));
console.log(result.data.data);
} else {
that.xxx = {};
that.tableDataer.length = 0;
}
})