前端多重嵌套的json数据格式解析

1,443 阅读1分钟

1.先上数据格式看看,这数据格式让我花费了近一上午才解决的

1.这是控制台带你出来的数据格式

applicableFormatsQt: "[{\"storeCode\":\"012002\",\
"storeName\":\"购物商场\",\
"dedicatedSiteFee\":\"4254.90\"}]"

2.这是复制过来的数据格式

"[{
"storeCode":"012002",
"storeName":"购物商场",
"dedicatedSiteFee":"4254.90"
}]"

3.控制台打印的多了很多 "\" 都是json的格式

解决方法

1.解析数据格式 ,这里需要注意的是return,要把次条数据格式放在获取api接口的最后一项,否则不会执行 循环只返回return所返回的值,并不会执行下面的值


 
//这里that.salesIncentive_tableData1是数组
that.salesIncentive_tableData1=JSON.parse(response.data.data.salesIncentive)//先将字符串转换成json格式
 var newArr = []; //声明空数组
 var json = []  // 声明空数组
 for(var i  in that.salesIncentive_tableData1 ){
 newArr[that.salesIncentive_tableData1[i]]  = json[i]
    }
    //循环遍历获取
   return newArr

2.进行赋值进行渲染 在这里插入图片描述 3.效果如下 在这里插入图片描述 4. 拿到数据的其中一项列如:拿数据的“storeName”

  var applicableFormats_other =JSON.parse(response.data.data.applicableFormatsQt)//解析数据格式
  var newArr = [];  //声明空数组
 for (let otherIndex = 0; otherIndex < applicableFormats_other.length; otherIndex++) {
  const element = applicableFormats_other[otherIndex];
    newArr.push(element.storeName);  //拿到我们数据里面的门店名字
    }
    //循环遍历
 that.ruleForm.applicableFormats_other=newArr.join(",");  //用逗号分割

综上就是解析多重数据格式的全部内容,希望对你有所帮助