Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there a really easy way I can take an array of JSON objects and turn it into an HTML table, excluding a few fields? Or am I going to have to do this manually?

share|improve this question

3 Answers 3

up vote 20 down vote accepted

I'm not sure if is this that you want but there is jqGrid. It can receive JSON and make a grid.

share|improve this answer
    
Another vote for jqGrid - really easy to implement, has great documentation and examples, and cuts down on a lot of the work for you. –  Peter Bailey Jun 26 '09 at 21:55
    
jqGrid looks very promising! –  Charlie Vieillard Nov 8 '13 at 11:07

Using jQuery will make this simpler.

The following will take an array of arrays and store convert them into rows and cells.

$.getJSON(url , function(data) {
    var tbl_body = "";
    $.each(data, function() {
        var tbl_row = "";
        $.each(this, function(k , v) {
            tbl_row += "<td>"+v+"</td>";
        })
        tbl_body += "<tr>"+tbl_row+"</tr>";                 
    })
    $("#target_table_id tbody").html(tbl_body);
});

You could add a check for the keys you want to exclude by adding something like

var expected_keys = { key_1 : true, key_2 : true, key_3 : false, key_4 : true };

at the start of the getJSON cbf and adding

if ( ( k in expected_keys ) && expected_keys[k] ) {
...
}

around the tbl_row += line.

Edit: Was assigning a null variable previously

share|improve this answer

You could use a jQuery plugin that accepts JSON data to fill a table. jsonTable

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.