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
+7.8k Golang : Scan files for certain pattern and rename part of the files
+16.6k Golang : Get IP addresses of a domain name
+9.3k Golang : How to check if a string with spaces in between is numeric?
+11.8k Golang : Secure file deletion with wipe example
+27.3k Golang : Find files by name - cross platform example
+5.3k Javascript : Change page title to get viewer attention
+7.2k Restart Apache or Nginx web server without password prompt
+9.4k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+15.8k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+6k Golang : Extract unicode string from another unicode string example
+17.7k Golang : Parse date string and convert to dd-mm-yyyy format
+12.2k Golang : Pagination with go-paginator configuration example