问题描述:

Why isn't the following working, inside a loop it never prints the url when myJSON is empty or not.

$.each($.parseJSON(myJSON), function(key,value){

alert(value.url);

});

for this JSON structure:

[{"host":"foo","url":"bar"},{"host":"foos","url":"bars"}]

Edit: $.each is inside a loop which has instances/iterations where myJSON is empty if that makes a difference.

网友答案:

This works for me.

var myJSON = '[{"host":"foo","url":"bar"},{"host":"foos","url":"bars"}]';

$.each($.parseJSON(myJSON), function(key,value){
    alert(value.url);
});
网友答案:
var data = [{"host":"foo","url":"bar"},{"host":"foos","url":"bars"}]

$.each(data, function(i, item) {
    alert(data[i].host);
});​

same as with the url.

相关阅读:
Top