Raw JSON is sent by the server as a string, not an object. And, although you can iterate through such strings, the hierarchical node-tree structure of the object is lost. So, by explicitly specifying the dataType:'json' in your AJAX request, the object is converted to a Javascript object which is iterable and with structure.

Finally, if you don’t convert the string within your AJAX request you’ll have to run JSON.parse() to convert it to an object. Conversely, JSON.stringify() converts a JSON object to a string.

The below illustrates looping through a JSON object with jQuery.


$.ajax({
    url: 'src/server.php',
    type: 'GET',
    dataType: 'json'
})
.done( output ) //because the function is called within the ajax request, 
                //the function parameter (json from server) is implicitly sent to it

function output( data ) {
    $.each(data, function (index, obj) { //This first loop iterates through 'rows' of json data
        console.log('row index: ' + index + '| length: ' + Object.keys(obj).length )

        $.each( obj, function ( key, value ) { //this second loop iterates through a single row, and thence 'columns', of json data
            console.log ('key: ' + key + ' sub_value: ' + value)
        });

    });

}