var teams = { "team1:" { "QB": "Alexander Hamilton", "RB": "John Jay", "WR": "James Madison" }, "team2: { "QB": "George Washington", "RB": "John Adams", "WR": "John Hancock" } };
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Window-target" content="_top" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Free Live Help!</title> <style id="css21" type="text/css" media="screen"> /* <![CDATA[ */ /* ]]> */ </style> <script id="javascript1.5" type="text/javascript"> // <![CDATA[ var Teams = function() { }; Teams.prototype = { team1 : { QB : "Alexander Hamilton", RB : "John Jay", WR : "James Madison" }, team2 : { QB : "George Washington", RB : "John Adams", WR : "John Hancock" } }; var teams = new Teams(); window.onload = function() { var div = (( document.getElementById ) ? document.getElementById("main") : document.all.main ); for ( var team in teams ) { div.innerHTML += "<h3>" + team + "</h3>\n<p>"; for ( var member in teams[ team ] ) { div.innerHTML += "Type : <span style=\"color : green\">" + member + "</span> - <span style=\"color : blue\">" + teams[ team ][ member ] + "</span><br />"; } div.innerHTML += "</p>"; } } // ]]> </script> </head> <body> <div id="main"></div> </body> </html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <script> var teams = { "team1": { "QB": "Alexander Hamilton", "RB": "John Jay", "WR": "James Madison" }, team2: { // key need not to be quoted "QB": "George Washington", RB : "John Adams", "WR": "John Hancock" } }; </script> </head> <body> The use of object teams: <br> <script> document.write(teams.team1.RB);document.write("<br>"); document.write(teams["team2"].WR);document.write("<br>"); document.write(teams['team1']["QB"]);document.write("<br>"); document.write("<br>"); for (var index in teams.team1) { document.write(index+" "+teams.team1[index]);document.write("<br>"); } document.write("<br>"); for (var teamix in teams) { for (var index in teams[teamix]) { document.write(teamix+" / "+index+" is: "+teams[teamix][index]);document.write("<br>"); } document.write("<br>"); } </script> </body> </html>
I get the following json object back from a json_encode command that I'm using on the server-side. Is it possible to parse through this using javascript since it is missing names of inner objects (team1 and team2). If so, how?
[ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ]
You could try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Window-target" content="_top" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>Free Live Help!</title> <style id="css21" type="text/css" media="screen"> /* <![CDATA[ */ /* ]]> */ </style> <script id="javascript1.5" type="text/javascript"> // <![CDATA[ var Teams = function() { }; Teams.prototype = { team1 : { QB : "Alexander Hamilton", RB : "John Jay", WR : "James Madison" }, team2 : { QB : "George Washington", RB : "John Adams", WR : "John Hancock" } }; var teams = new Teams(); window.onload = function() { var div = (( document.getElementById ) ? document.getElementById("main") : document.all.main ); for ( var team in teams ) { div.innerHTML += "<h3>" + team + "</h3>\n<p>"; for ( var member in teams[ team ] ) { div.innerHTML += "Type : <span style=\"color : green\">" + member + "</span> - <span style=\"color : blue\">" + teams[ team ][ member ] + "</span><br />"; } div.innerHTML += "</p>"; } } // ]]> </script> </head> <body> <div id="main"></div> </body> </html>
innerHTML method.
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
I getting a better understanding of parsing throuhg objects. Quick question. Wy won't the following work. I'm getting undefined when I try and execute the code.
for(var player in players) document.write(player["College"]);
Hi Gary,
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using theinnerHTMLmethod.
All codes is as follows:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
hope it helps...
[ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ][ {"PlayerName":"Ron Artest","Position":"Forward","Height":"6-7","Weight":"260","College":"St. Johns"}, {"PlayerName":"Kobe Bryant","Position":"Guard","Height":"6-6","Weight":"205","College":"Lower Marion High Sc"} ] [{"TeamLogo":"C:\\wamp\\wwwNBA\\laLakersLogo.gif"}]
Hi Gary,
here's your code, i've extrcted all of its collections with the used of a table, but if don't like my concept of using table to differentiate the items inside the players collections -- then simply re-apply the same procedure in my original post, using theinnerHTMLmethod.
All codes is as follows:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- W3Cs Standard Template : XHTML 1.0 Transitional DTD --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>www.daniweb.com</title> <style type="text/css"> /* <![CDATA[ */ table { border-collapse : collapse; border : 1px solid #C0C0C0; color : #365895; text-align : center; width : 100%; } caption { width : auto; text-align : left; margin-bottom : .500em; padding-left : .500em; } td { border-top : 1px solid #C0C0C0; border-right : 1px dashed #C0C0C0; padding : .350em; width : auto; vertical-align : middle; letter-spacing : 2px; } thead td { background-color : #F0FFFF; border-right : 1px dashed #C0C0C0; padding : .350em; font : bold 80%/1 "Trebuchet MS", Verdana, Arial, sans-serif; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ window.onload = function() { if ( document.createElement ) { var players = [ { PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : 260, College : "St. Johns" }, { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : 205, College : "Lower Marion High Sc" } ]; var container = document.getElementById("view"); var labels = [ "Name", "Position", "Height", "Weight", "College" ]; var table = document.createElement("table"); var tCaption = table.createCaption(); tCaption.appendChild( document.createTextNode("NBA :: Allstars Statistics-Table" )); var tHead = table.createTHead(); var addCell = function( row, label ) { return row.insertCell( -1 ).appendChild( document.createTextNode( label )); }; var xrow = tHead.insertRow( -1 ); for ( var i = 0; labels[ i ]; i++ ) { addCell( xrow, labels[ i ] ); } var tBody = document.createElement( "tbody" ); table.appendChild( tBody ); for ( i = 0; players[ i ]; i++ ) { xrow = tBody.insertRow( -1 ); for ( var info in players[ i ] ) { addCell( xrow, players[ i ][ info ] ); } } container.appendChild( table ); return; } alert( "unsupported features, please update your browser" ); return false; } // ]]> </script> </head> <body id="xhtml-10-transitional"> <div id="view"></div> <!-- id :: view --> </body> </html>
hope it helps...
Thanks,
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for(var player in players) document.write(player["College"]);
document.writeln method, and created a table to differentiate each of the item inside the teams object variable.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <title>W3C HTML 4.01 loose DTD</title> <style type="text/css"> <!-- td { border-right : 1px solid #A0A0A0; border-top : 1px solid #A0A0A0; background-color : #F1F1F1; color : #405060; } th:first-child { border-bottom : 1px solid #A0A0A0; border-right : 1px solid #A0A0A0; background-color : #800090; color : yellow; width : 25%; } table { border : 1px solid #A0A0A0; border-collapse : collapse; text-align : left; width : 100%; } // --> </style> <script type="text/javascript"> <!-- var teams = [ { "LA Lakers" : { "Team Logo" : "C:\\wamp\\wwwNBA\\laLakersLogo.gif", Players : { 37 : { // Jersey n# PlayerName : "Ron Artest", Position : "Forward", Height : "6-7", Weight : "260", College : "St. Johns" }, 24 : { PlayerName : "Kobe Bryant", Position : "Guard", Height : "6-6", Weight : "205", College : "Lower Marion High Sc" } } } } // Add More NBA teams below >>> ]; // --> </script> </head> <body> <noscript> <p> This site requires a browser that support <b>JavaScript</b>. </p> </noscript> <div id="nbastats"> <script type="text/javascript"> <!-- ( function() { var tLen = teams.length; var table = '<table id="tableOne" border="0" cellpadding="5" cellspacing="0" frame="void" rules="none" summary="NBA Statistic Table">\n'; for ( var x = 0; x < tLen; x++ ) { table += "<tr>\n"; for ( var teamName in teams[ x ] ) { table += "<th title=\"" + teamName + "\" colspan=\"3\">Team Name: </th>\n"; table += "<td colspan=\"3\">" + teamName + "</td>\n"; table += "</tr>\n"; table += "<tr>\n"; for ( var misc in teams[ x ][ teamName ] ) { table += "<th colspan=\"3\">" + misc + "</th>\n"; } table += "</tr>\n"; table += "<tr>\n"; var count = 10; var image = new Image(); image.src = teams[ x ][ teamName ][ "Team Logo" ]; table += "<td style=\"background-color : #FFFFFF;\" rowspan=\"" + ( count += ( 1 * 10 )) + "\" colspan=\"3\"><img src=\"" + image.src + "\" alt=\"" + image.src + "\"></td>"; table += "</tr>\n"; for ( var j in teams[ x ][ teamName ][ misc ] ) { for ( var player in teams[ x ][ teamName ][ misc ][ j ] ) { table += "<tr>\n"; table += "<td style=\"text-align : center; background-color : #E0E0E0\">" + player + "</td><td>" + teams[ x ][ teamName ][ misc ][ j ][ player ] + "</td>\n"; table += "</tr>\n"; } table += "<tr>\n"; table += "<td>Jersey no.</td><td>#" + j + "</td>\n"; table += "</tr>\n"; table += "<tr>\n"; table += "<td colspan=\"2\" style=\"background-color : #800090; height : 5px;\"> </td>\n"; table += "</tr>\n"; } } } table += "</table>"; document.writeln( table ); } )( /* NBA Teams & Players */ ) // --> </script> </div> </body> </html>
| DaniWeb Message | |
| Cancel Changes | |