没啥好写的再水一篇
json中都是字符串,转换一下就好,简单来说就是转换一次不行,再转换换一次,整体转换不够,细分的再转换一次
这是vue中 ts写法 ,我这里是拿对象做对比,不好字符和对象做对比,转换成一个层级对比
const dataMap =()=> {
let data: {} as CurrentBasic, //这是接收的对象
let jsonData = JSON.parse(props.option.data) //传递的是对象就不用转换
for (let key in data) {
if (jsonData .hasOwnProperty(key)) {
let val= (jsonData as Record<string,any>)[key];
try{
(data as Record<string,any>)[key] = JSON.parse(val);
}
catch{//有些文字或者其他类型会转换失败,那就直接赋值
(data as Record<string,any>)[key] = val;
}
}
}
}
简单来说就是
const dataMap =()=> {
let data: {} as CurrentBasic,
let jsonData = JSON.parse(str)
for (let key in state.data) {
if (jsonData .hasOwnProperty(key)) {
let val= str[key];
data[key] = JSON.parse(val);
}
}
}