关于Json中一个json对象包裹着一个json数组,这样的方式怎么解析数据,并传给前端

项目场景:

部分业务会涉及到其它服务器,会将自己数据上传到该服务器,然后根据上传的数据获取反馈的数据进行数据渲染,这个时候数据也许是根据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;

					}
				})

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值