JSON读取问题

发布于 7 年前作者 fang013974 次浏览最后编辑 7 年前来自 ask
console.log(res.data);
 
console.log(res.data.message);
 
console.log(res.data.code);

分别打印3条数据,结果如下:

{"code": 200, "message": "success"}
undefined
undefined

突然这个json就读不到数据了,求大神帮忙看看这是啥情况

5 回复
liuping
liuping1 楼6 年前

我也是用这个,可以转

liaoxiuying
liaoxiuying2 楼6 年前

字符串不是JSON对象,没法通过key读value。

把字符串parse成JSON对象就行了。

rding
rding3 楼6 年前

像是异步的问题,打印放在接口请求成功的方法里试试

xiulan38
xiulan384 楼6 年前

res.data  这个是字符串吧

得转json

yinfang
yinfang5 楼5 年前

let obj = JSON.parse(res.data);

console.log(obj.message);