Skip to content Skip to sidebar Skip to footer

How To Print The Associative Array In Print_r() Fashion From A $.getjson Call?

simple thing. $.getJSON is used and the result from the .php file being called is caught as response in the jquery callback function. The json encoded php array is an associative a

Solution 1:

I believe console.dir() is what you're looking for:

https://developer.mozilla.org/en-US/docs/Web/API/Console.dir

The only downside is it doesn't allow for labeling each object that's output, and it's also a non-standard console method.

Solution 2:

If you have Chrome or Firebug, you can use console.log(response). In the console, you can click on the logged object to view the properties of it.

Solution 3:

Use something like Doug Crockford's JSON library to transform the response to text and log it with console.log

function(response) {
    console.log(JSON.stringify(response));
}

Solution 4:

$.getJSON("file.php", function(data) {
    var items = [];

    $.each(data, function(key, val) {
        items.push('<li id="' + key + '">' + val + '</li>');
    });

    $('<ul/>', {
        'index': 'my-list',
        html: items.join('')
    }).appendTo('body');
});

Using this structure, the example loops through the requested data, builds an unordered list, and appends it to the body.

IF you desire an alternative format you could modify the each such as:

 $.each(data, function(key, val) {
       alert('key:' + key + ' value:' + val);
    });

I admit I borrowed from: http://api.jquery.com/jQuery.getJSON/

Post a Comment for "How To Print The Associative Array In Print_r() Fashion From A $.getjson Call?"