Solved

Show the JSON object into a tabular data

Posted on 2011-02-23
8
488 Views
Last Modified: 2013-11-08
How to format this JSON format returned thru Java Object into an HTML table.

{
  "transactions":
  [
    {
      "error": "",
      "ctrNum1": "123456789",
      "successful": true
    },
    {
      "error": "",
      "dtcControlNumber": "123456790",
      "successful": true
    },
    {
      "error": "",
      "ctrNum2": "123456791",
      "successful": true
    },
    {
      "error": "",
      "ctrNum3": "123456792",
      "successful": true
    },
    {
      "error": "",
      "ctrNum4": "123456793",
      "successful": true
    }
  ]
}
0
Comment
Question by:mahaq
  • 4
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34966909
Check this :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	var json = {"transactions":[{"error":"","ctrNum1":"123456789","successful":true},{"error":"","dtcControlNumber":"123456790","successful":true},{"error":"","ctrNum2":"123456791","successful":true},{"error":"","ctrNum3":"123456792","successful":true},{"error":"","ctrNum4":"123456793","successful":true}]};
	window.onload = function() {
		
		// create table
		var table = document.createElement("table");
		table.setAttribute("border", "1");

		// create and add the header row
		var transaction = json.transactions[0];
		var row = table.insertRow(table.rows.length);
		for(var j in transaction) {
			var cell = row.insertCell(row.cells.length);
			cell.appendChild(document.createTextNode(j));
		}

		// create the rows of data
		for(var i=0;i<json.transactions.length;i++) {
			var transaction = json.transactions[i];
			var row = table.insertRow(table.rows.length);
			for(var j in transaction) {
				var cell = row.insertCell(row.cells.length);
				cell.appendChild(document.createTextNode(transaction[j]));
			}
		}

		// put the table in the body of the page
		document.getElementsByTagName("body")[0].appendChild(table);

		// if you want to put the table in a div
		// document.getElementById("divId").appendChild(table);
	}
</script>
</head>
<body>
<div id="divId"></div>
</body>
</html>

Open in new window

0
 

Author Comment

by:mahaq
ID: 34966977
How can I do a jquery DOM ready instead of body onload?.

Thanks for the help, looks ok, I'll have to see it on the live environment.



0
 

Author Comment

by:mahaq
ID: 34966980
Adding to it, can this DOM be appended in jquery way.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34966996
>How can I do a jquery DOM ready instead of body onload?.

line 8 : $(document).ready(function() {
line 37 : });

>Adding to it, can this DOM be appended in jquery way.
Yes :
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 82

Expert Comment

by:leakim971
ID: 34967018
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script language="javascript">
	var json = {"transactions":[{"error":"","ctrNum1":"123456789","successful":true},{"error":"","dtcControlNumber":"123456790","successful":true},{"error":"","ctrNum2":"123456791","successful":true},{"error":"","ctrNum3":"123456792","successful":true},{"error":"","ctrNum4":"123456793","successful":true}]};
	$(document).ready(function() {
		
		// create table
		$("<table border=\"1\"><tr></tr></table>").appendTo("body");
//		$("<table border=\"1\"><tr></tr></table>").appendTo("#divId");

		// create header row
		var transaction = json.transactions[0];
		for(var j in transaction) {
			$("table tr").append("<td>" +  j + "</td>");
		}

		// create body
		for(var i=0;i<json.transactions.length;i++) {
			var transaction = json.transactions[i];
			$("table").append("<tr />");
			for(var j in transaction) {
				$("table tr:eq(" + ($("table tr").length-1) + ")").append("<td>" + transaction[j] + "</td>");
			}
		}

	});
</script>
</head>
<body>
<div id="divId"></div>
</body>
</html>

Open in new window

0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34968006

Here's another version to consider:

<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26843230.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script>

var status = {
  "transactions": 
  [
    {
      "error": "",
      "ctrNum1": "123456789",
      "successful": true
    },
    {
      "error": "",
      "dtcControlNumber": "123456790",
      "successful": true
    },
    {
      "error": "",
      "ctrNum2": "123456791",
      "successful": true
    },
    {
      "error": "",
      "ctrNum3": "123456792",
      "successful": true
    },
    {
      "error": "",
      "ctrNum4": "123456793",
      "successful": true
    }
  ]
};

jQuery(document).ready(function() {

  var row = $("<tr/>");
  for ( var column in status.transactions[0] ) {
    row.append( $("<th/>").html( column ) );
  }
  $("#status").append( row );

  for ( var index in status.transactions ) {
    var row = $("<tr/>");
    for ( var column in status.transactions[index] ) {
      var cell = status.transactions[index][column];
      cell = ( cell.toString() != "" )? cell : " ";
      row.append( $("<td/>").text( cell ) );
    }
    $("#status").append( row );
  }

});

</script>
</head>
<body>

<table id="status" border="1"></table>

</body>
</html>

Open in new window

0
 

Author Comment

by:mahaq
ID: 34973958
Convert this json string and display table thru the java script dynamically instead of DOM based.
It has to be on the fly. Is it possible?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34974901
>It has to be on the fly. Is it possible?

Of course. Just replace the current event $(document).ready(function() {
by the new one, for example a click on a button : $("#myButtonId").click(function() {
or any other event... an other one, callback function : $.get("/path/to/loadJSON.php", function(json) {
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now