简书链接:typescript与小程序的resdatajson对象解析对应的类型
文章字数:48,阅读全文大约需要1分钟
下面代码是处理服务器是脏数据json用()包裹的情况以及纯json的情况 ,json对象类型打印是object直接转换为类型Record<string, any>即可。

1
2
3
4
5
6
7
8
9
if (typeof (res.data) == "string") {
var result = res.data.replace(/\(|\)/g, "");
console.debug("result:" + result);
var obj: object = JSON.parse(result);
console.debug("obj:" + (typeof obj));
datax = obj as Record<string, any>;
} else {
datax = res.data as Record<string, any>;
}