Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function parseText(str) {
- try{
- var resObj = jQuery.parseJSON(str);
- generateTable(resObj);
- }catch(e){
- if(e instanceof SyntaxError) {
- alert("There was a syntax error in your JSON string.\n" + e.message + "\nPlease check your syntax and try again.");
- jQuery("jsonText").focus();
- return;
- }
- alert("There was an unknown error. Perhaps the JSON string contained a deep level of nesting.");
- jQuery("jsonText").focus();
- return;
- }
- }
- function generateTable (obj) {
- var table, tbody, theader, tr, td, th, rowB, rowH;
- table = jQuery('<table class="table"><thead></thead><tbody></tbody></table>');
- thead = table.children('thead');
- tbody = table.children('tbody');
- tr = '<tr></tr>';
- th = '<th></th>';
- td = '<td></td>';
- $.each(obj, function (ind, sub) {
- if(typeof sub === 'object'){
- if(ind === 0){
- rowH = $(tr);
- }
- rowB = $(tr);
- $.each(sub, function (key, val) {
- if(ind === 0){
- rowH.append($(th).text(key));
- }
- rowB.append($(td).text(val))
- });
- rowB.appendTo(tbody);
- }
- });
- rowH.appendTo(thead);
- $('#main').append(table);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement