Json Data in PHP

JSON stands for JavaScript Object Notation
JSON is lightweight text-data interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand

Example:

/*
*JsonTest.php
*/
<?php
$employees
= array( array( 'firstName' => 'John', 'lastName' => 'Doe' ),
                    array(
'firstName' => 'Anna', 'lastName' => 'Smith' ),
                    array(
'firstName' => 'Peter', 'lastName' => 'Jones' )
                    );
 
echo
json_encode($employees);
exit();
//Always use Exit to Close Page.
?>

output:

[ { "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]

/*
*JsonTest.html
*/

<html>
  <head>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
      var MyData = '';
      var url = 'JsonTest.php';
      $.get(url, function(data) {
      var result = eval('(' + data + ')');
      $.each( result, function( index, value ) {
      MyData += 'Fist Name : '+ value['firstName']+ "  Last Name : " + value['lastName'] +"<br />";
      });
      $('#display').html(MyData);
      });
     
    </script>
  </head>
  <body>
<H1>Hello World</H1>
<div id="display">
</div>
  </body>
</html>

Output:
Hello World
Fist Name : John Last Name : Doe
Fist Name : Anna Last Name : Smith
Fist Name : Peter Last Name : Jones

Tags: 
X