Wednesday, December 24, 2014

Encoding and Decoding JSON data

JSON is a  kind of data exchanging format ,encoding and decoding  can be done in various web programming languages using JSON like PHP, ASP.NET etc. Here is a basic example on PHP about  encoding and decoding .

Encoding JSON data in PHP :




Example: 

PHP File (encode.php)
 <?PHP  
 header('Content-type: application/json');  
 $json = array('PHP' => 1, 'JSON' => 2, 'HTML' => 3, 'JavaScript' => 4, 'jQuery' => 5);  
 echo json_encode($json);  
 ?>  

PHP File (decode.php)
 <?PHP  
 $json='{"PHP":1,"JSON":2,"HTML":3,"CSS":4,"JavaScript":5,"jQuery":6}';  
 echo "<pre>";  
 var_dump(json_decode($json));  
 var_dump(json_decode($json,true));  
 echo "</pre>";  
 ?> 

No comments:

Post a Comment