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
+8.6k Golang : GMail API create and send draft with simple upload attachment example
+19.5k Golang : Measure http.Get() execution time
+4.8k Golang : Display packages names during compilation
+5.8k Linux/MacOSX : Search for files by filename and extension with find command
+16.1k Golang : Check if a string contains multiple sub-strings in []string?
+10.7k Golang : Sieve of Eratosthenes algorithm
+17.4k Convert JSON to CSV in Golang
+5.8k Javascript : Get operating system and browser information
+12.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+10.2k Android Studio : Simple input textbox and intercept key example
+5.2k Golang : fmt.Println prints out empty data from struct
+4.8k Golang : micron to centimeter example