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.
Review a simple jQuery example to loop over a JavaScript array object. var json = [ {"id":"1","tagName":"apple"}, {"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"}, {"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"} ]; $. each(json, function(idx, obj) { alert(obj. tagName); });
each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.
jQuery Code:each(myArray, function (index, value) { console. log(index+' : '+value); }); Output: So here is the output which displays index and values, by a loop through arrays.
$. ajax({ type: "POST", url: "/Customers/GetCustomer", data: { Id: 1 }, dataType: 'json', success: function (response) { for (var i = 0; i < response. CustomerList.
This works for me.
var myJSON = '[{"host":"foo","url":"bar"},{"host":"foos","url":"bars"}]';
$.each($.parseJSON(myJSON), function(key,value){
alert(value.url);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With