JSON in PHP


JSON or JavaScript Object Notation is a lightweight data-interchange format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. This is language independent ,self-describing and easy to understand and implement specially android->json->webserver .


● JSON in PHP

json


Here is an example to illustrate JSON implementation

<!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>
</head>
<body>

<!--JSON Object javaSript -->
<script type="text/javascript">
    	
		var x=100;
		document.writeln(x);
		
		var  info={ no:"101",name:"Shailesh Dwivedi",City:"HP Dubey Marg, Betiahata,Gorakhpur"};
			document.writeln("<br/>----------------------------------");
			document.writeln("<br/>Roll Number::"+info.no);
			document.writeln("<br/>Name::"+info.name);
			document.writeln("<br/>City::"+info.City);
			document.writeln("<br/>----------------------------------<br/>");
		

</script>



<!--convert  JSON object from PHP Array -->
 <?php 
 		
 		$arr=array( "no"=>"101","name"=>"Shailesh Dwivedi","City"=>"HP Dubey Marg, Betiahata,Gorakhpur");
		echo "<br/> Roll Number::".$arr["no"];
		echo "<br/> Name::".$arr["name"];
		echo "<br/>City::".$arr["City"];
		echo "<br/>".json_encode($arr);
		
?>



<!--Accessing JSON encode PHP Array to javaSript -->
<script  type="text/javascript">
		 
		 var data=<?php echo json_encode($arr); ?>;
		 document.writeln("<br/>----------------------------");
			document.writeln("<br/>Roll Number::"+data.no);
			document.writeln("<br/>Name::"+data.name);
			document.writeln("<br/>City::"+data.City);
			document.writeln("<br/>----------------------------<br/>");
</script>

<!--  Accessing Database  and jSON encode -->
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'shop';
$db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$sql="SELECT * FROM product  where pid=103 ORDER by pid";
$r=$db->query($sql);
$r->setFetchMode(PDO::FETCH_ASSOC);
$row = $r->fetch();
echo "<br/>".$row["PID"]."|".$row["PNAME"]."|".$row["PRICE"];
echo "<br/>".json_encode($row);
?>
		<script  type="text/javascript">
		 var data=<?php echo json_encode($row); ?>;
		 document.writeln("<br/>----------------------------");
		 document.writeln("<br/>PID::"+data.PID);
		 document.writeln("<br/>PNAME::"+data.PNAME);
		 document.writeln("<br/>PRICE::"+data.PRICE);
		 document.writeln("<br/>----------------------------<br/>");
</script>
</body>
</html>

● output

100 
---------------------------------- 
Roll Number::101 
Name::Shailesh Dwivedi 
City::HP Dubey Marg, Betiahata,Gorakhpur 
----------------------------------

Roll Number::101
Name::Shailesh Dwivedi
City::HP Dubey Marg, Betiahata,Gorakhpur
{"no":"101","name":"Shailesh Dwivedi","City":"HP Dubey Marg, Betiahata,Gorakhpur"} 
---------------------------- 
Roll Number::101 
Name::Shailesh Dwivedi 
City::HP Dubey Marg, Betiahata,Gorakhpur 
----------------------------

103|TV 42" Panasonic|45000
{"PID":"103","PNAME":"TV 42" Panasonic","PRICE":"45000"}	
---------------------------- 
PID::103 
PNAME::TV 42" Panasonic 
PRICE::45000 
----------------------------