javascript - jQuery: Loop through JSON document -
i have following json structure on "y" location:
{"results":[{ "a_id":4529245, "type":"a", "name":"honda" },{ "a_id":1234567, "type":"l", "name":"autos marron" }]}
inside js document have following code:
var i; i=0; $.getjson('/document/location', function( data ) { console.log(data); $.each( data, function( key, val ) { var contentstring = '<span>'+ val[i].name + '</span>'; $('#info').append(contentstring); i++; }); });
i searched online , readed able val.name , able print each of "name" variables inside json document.
but have use incrementing variable (i) in order print only first variable equals "honda" using val[i].name
i'd print variables called name. doing wrong?
thanks in advance
you need loop data.results
array want access:
code
$.getjson('/document/location', function( data ) { console.log(data); $.each( data.results, function(key,val ) { var contentstring = '<span>'+ val.name + '</span>'; $('#info').append(contentstring); }); });
** in code when loop through data get:
val
[{ "a_id":4529245, "type":"a", "name":"honda" },{ "a_id":1234567, "type":"l", "name":"autos marron" }]
and key results
now since val array display name had data[index].name
edit:
here working fiddle
Comments
Post a Comment