kamesh
kamesh

Reputation: 2424

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

Upvotes: 0

Views: 62

Answers (1)

Satpal
Satpal

Reputation: 133403

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);
});

Upvotes: 2

Related Questions