项目场景:
将数据上传到web服务器,通过条件查询混并渲染
问题描述
数据能查到,也有返回的json,但是数据渲染不到前端页面数据全部为null
原因分析:
后端解析数据有问题,例如一个json对象和json数组,数组使用了对象的方式进行操作,肯定解析不到数据
解决方案:
先看json的格式是什么样如果看不出来,可以利用工具将json解析出来(工具连接在下面),获取到json之后就可以解析数据了
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 = xxx根据自己的需求填写json或者是服务器连接即可
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;
}
我这里是一个对象包含着一个数组,所以我要把数组取出来,并放到一个集合或者数组里,不然的话数据会渲染失败,补充一下,,不同的类型上传的函数不一样,我这里的list使用add进行赋值然后将这个数据返回到前端进行解析然后渲染
前端:可以看到我使用了data,data[0]和data[1],这样才能将两条数据的内容取出来并且渲染到对应的界面
mdtrtinfoQuerys() {
let that = this;
//服务器后端地址
that.$axios.defaults.baseURL = 'http://localhost:xxxx';//写自己的路径
that.$axios({
method: 'get',
url: xxx这里的参数自己定
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.queryInfos = {};
that.tableDataer.length = 0;}
})