Forum Moderators: coopster

Message Too Old, No Replies

parse json file in php

displaying it in the form of table

         

Silent Miracle

8:14 am on Jul 27, 2011 (gmt 0)

10+ Year Member



i am trying to parse a json file in php and display the result in the form of a html table.i searched about it on the internet and found this code :
<?php
$json=file_Get_contents("new.json");
$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key.:\n ";

} else {

echo "$key => $val\n";
}
echo "<br/>";
}
?>
it successfully parses and displays the result but i could not manage to display it in the form of table. i tried doing that and came up with this code but this doesnot work either :

<?php
$json=file_Get_contents("new.json");
$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);
echo "<table border=\"1\">";
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "<tr>";
echo " <td>.$key.:\n </td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "$key => $val\n";
echo "</td>";
echo "</tr>";
}
echo "<br/>";
}
echo "</table>";
?>
please help!

rocknbil

3:37 pm on Jul 27, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have some quoting problems (probably get an error.)

Second this

echo "$key => $val\n";

Is invalid in an echo, you're already setting $key and $value in the for loop.

Third, if this works you'll have a column mismatch (see colspan=\"2\")

Fouth, scalar (string, as opposed to array) variables **will** interpolate when double quoted - no need for concatenation. Hence when I add colspan=\"2\" the quotes must be escaped or PHP will think the string ends at the first " before "2".

Last, <br> (or <br/> if you MUST use XHTML) is invalid between table rows. Use CSS to style more space if you need it.

Bonus answer: error trapping and alternating row colors added. :-)

$light = '#fff';
$tint = '#c0c0c0';
$bg=null;
<?php
$json=file_Get_contents("new.json");
$jsonIterator = new
RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST); // multiple lines only for this forum
if (count($jsonIterator) > 0) {
echo "<table border=\"1\">\n";
foreach ($jsonIterator as $key => $val) {
$bg = ($bg==$light)?$tint:$light;
if(is_array($val)) {
echo "<tr><td style=\"background:$bg\" colspan=\"2\">$key</td> </tr>\n";
}
// If it's a **list** array, this might be better - remove the above and uncomment this. If
// it's an associative array, you'll neet to do the
// same thing you're doing in the outer loop:
//if(is_array($val)) {
// $cnt = count($val);
// echo "<tr><td style=\"background:$bg\">$key</td><td>";
// for ($j=0;$j<$cnt;$j++) { echo " " . $val[$j]; }
// echo "</td></tr>\n";
//}
else {
echo "<tr><td style=\"background:$bg\">$key</td><td style=\"background:$bg\">$val</td></tr>\n";
}
}
echo "</table>\n\n";
}
else { echo "<p>The iterator is empty.</p>"; }
?>

Silent Miracle

5:43 am on Jul 28, 2011 (gmt 0)

10+ Year Member



ohhh ok ...thanks ... yes the output looks quite beautiful :) Thanks for you help... now i will try to add more functionalities in this code...

rocknbil

4:05 pm on Jul 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah except those three variables at the top need to go inside the <?php delimiter . . . otherwise it should work.