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
+6.9k Golang : Gomobile init produce "iphoneos" cannot be located error
+4.8k List of Golang XML tutorials
+6.2k Golang : Check if one string(rune) is permutation of another string(rune)
+11.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+16.5k Convert JSON to CSV in Golang
+37.7k Golang : How to read CSV file
+19.2k Golang : Underscore or snake_case to camel case example
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.2k Golang : How To Use Panic and Recover
+11.7k Python : Convert IPv6 address to decimal and back to IPv6
+5.4k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+10.4k Golang : Generate DSA private, public key and PEM files example