在js中如何取出json中的对象的值
现有一个从PHP端返回的json(变量名为gets):{"code":0,"data":[{"uid":"689791","username":"vaghyuzhihao...
现有一个从PHP端返回的json(变量名为gets):
{"code":0,"data":[{"uid":"689791","username":"vaghyuzhihao","orgid":"627","status":"1","subname":"yuzhihao","roleids":"1","orgcode":"600251","name":"yuzhihao","mapid":"0","passwd":"","siteid":"0","createorgcode":"600251","alarmview":"0"}]}
请取出data中uid的值
我是这么写的:alert(gets.uid);
返回的是undefined
请问该怎么取 在线等! 展开
{"code":0,"data":[{"uid":"689791","username":"vaghyuzhihao","orgid":"627","status":"1","subname":"yuzhihao","roleids":"1","orgcode":"600251","name":"yuzhihao","mapid":"0","passwd":"","siteid":"0","createorgcode":"600251","alarmview":"0"}]}
请取出data中uid的值
我是这么写的:alert(gets.uid);
返回的是undefined
请问该怎么取 在线等! 展开
4个回答
展开全部
你这个是个字符串。JSON.parse一下就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var str = {"code":0,"data":[{"uid":"689791","username":"vaghyuzhihao","orgid":"627","status":"1","subname":"yuzhihao","roleids":"1","orgcode":"600251","name":"yuzhihao","mapid":"0","passwd":"","siteid":"0","createorgcode":"600251","alarmview":"0"}]};
var json = eval(str);
console.log(json.data[0].uid);
一般返回的都是字符串,需要对其对象化
追答
var json;
var str = {"code":0,"data":[{"uid":"689791","username":"vaghyuzhihao","orgid":"627","status":"1","subname":"yuzhihao","roleids":"1","orgcode":"600251","name":"yuzhihao","mapid":"0","passwd":"","siteid":"0","createorgcode":"600251","alarmview":"0"}]};
if(typeof str === 'object'){
json = str;
}
else{
json = eval(str);
}
console.log(json.data[0].uid);
试试这个,判断输入的是否为object,是则直接输出,不是则将其转换成object再输出
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你前台是用什么接收的?
追问
js
追答
2楼正解!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询