简体   繁体   中英

Error in accessing json data

This is my ajax code

$.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

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

$.each(tem[test],function(index, value){ 
    alert(value.name);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM