[英]Error in accessing json data
This is my ajax code 这是我的ajax代码
$.ajax({
url:"jsoncontent.json",
dataType:"json",
success:function(data){
var tem = data;
var test ='facebook';
alert(tem.facebook[0].name);//working
alert(tem.test[0].name);//Why it is not Working?How can i access with test variable
//alert(tem.test+[2].name);tried
}
});
I got confused in accessing json data.. any help 我在访问json数据时感到困惑..任何帮助
You need to use Bracket notation . 您需要使用括号表示法 。
Thus use 因此使用
alert(tem[test][0].name);
instead of 代替
alert(tem.test[0].name);
EDIT: As per comments. 编辑:根据评论。 You should visit official jQuery.each() docs
您应该访问官方的jQuery.each()文档
$.each(tem[test],function(index, value){
alert(value.name);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.