Javascript : How to loop over and parse JSON data?
This note is a short tutorial on how to loop over and parse JSON data with JQuery's each()
function and also show example on how to use JQuery's each()
function callback.
jQuery.each( array, callback )
In this example below, the callback function will treat the id as index and alphabet as object.
Here we go :
var jsonArray = [{
"id": "1",
"alphabet": "a"
}, {
"id": "2",
"alphabet": "b"
}, {
"id": "3",
"alphabet": "c"
}, {
"id": "4",
"alphabet": "d"
}, {
"id": "5",
"alphabet": "e"
}];
$.each(jsonArray, function(index, object) {
alert(object.alphabet);
});
Play at : http://codepen.io/anon/pen/JdeyZr
Sometimes, JQuery will complain(in console log) that the JSON array is not properly formatted. This is usually caused by malformed JSON string. To fix this issue, use the JQuery's parseJSON()
function to parse the JSON array.
$.each($.parseJSON(jsonArray), function(index, object) {
alert(object.alphabet);
});
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13k Golang : convert rune to unicode hexadecimal value and back to rune character
+15.7k Golang : File path independent of Operating System
+47.3k Golang : Upload file from web browser to server
+4.7k Golang : Stop goroutine without channel
+26.7k Golang : Record voice(audio) from microphone to .WAV file
+2.3k Golang : Fix go-cron set time not working issue
+19.3k Nginx + FastCGI + Go Setup.
+12k Python : Convert IPv6 address to decimal and back to IPv6
+6k Golang : Muxing with Martini example
+10k PHP : Convert(cast) bigInt to string
+14.3k Golang : How do I get the local IP (non-loopback) address ?
+12.5k Golang : reCAPTCHA example