• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

JSON Tutorials

  • What is Json
  • Json Example
  • Json Object Example
  • Json with php
  • How to Convert Data from MySQL to JSON using PHP
  • How to store json Data into MySQL database using PHP
  • How to insert json data into mysql database using PHP
  • validate json string in php
  • Json with AJAX
  • Json with javascript
  • Json Program example

 Important Links

Read More

Home » JSON »  About Json

JSON Program Example.


Example :-

File name : index.php

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
//var_dump(json_decode($json));
//var_dump(json_decode($json, true));
$ar1 = json_decode($json);
//print_r($ar1);
foreach ($ar1 as $key => $val)
{
echo $key." ". " = " .$val;
echo "<br/>";
}
$ar2 = json_decode($json, true);
foreach ($ar2 as $key => $val)
{
echo $key." ". " = " .$val;
echo "<br/>";
}
?

Output :-

a = 1 b = 2 c = 3 d = 4 e = 5
a = 1 b = 2 c = 3 d = 4 e = 5

Example :-

File name : index.php

<?php
$json = array("mahtab","alam","kalam","asif");
$ar = json_encode($json);
echo $ar;
echo "<br/>";
$de = json_decode($ar);
print_r($de);

?>

Output :-

/*
["mahtab","alam","kalam","asif"]
Array ( [0] => mahtab [1] => alam [2] => kalam [3] => asif )

*/

Example :-

File name : index.php

<?php
$json = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$ar = json_encode($json);
echo $ar;
echo "<br/>";
$de = json_decode($ar);
print_r($de);

?>

Output :-

{"Peter":"35","Ben":"37","Joe":"43"}
stdClass Object ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )

Example :-

File name : index.php

<?php
include 'db.php';
$sql = "select * from apps_details";
$result = mysqli_query($conn,$sql);
$json = array();
if(mysqli_num_rows($result)){
while($row=mysqli_fetch_row($result)){
$json['apps_details'][]=$row;
}
}
mysqli_close($conn);
echo json_encode($json);
?>

Output :-


Example :-

File name : index.php

<?php
$json = '{ "MovieDatabase": {
"movie": [
{ "name":"The Change-Up",
"genre": "comedy",
"director": "David Dobkin",
"Facebook_like": 252
},
{ "name":"Rise of the Planet of the Apes",
"genre": "SciFi",
"director": "Rupert Wyatt",
"Facebook_like": 472
},
{ "name":"30 Minutes or Less",
"genre": "adventure",
"director": "Ruben Fleischer",
"Facebook_like": 114
},
{ "name":"Final Destination 5",
"genre": "Horror",
"director": "Steven Quale",
"Facebook_like": 241
}
]
}
}';


$de = json_decode($json);
print_r($de);


foreach ($de as $data => $value)
{
echo $data;
echo "<br/>";
print_r($value);




}





?>

Output :-


Example :-

File name : index.php

Encoding:
$json_data = array ('id'=>1,'name'=>"rolf",'country'=>'russia',"office"=>array("google","oracle"));
echo json_encode($json_data);

Decoding:
$json_string='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} ';
$obj=json_decode($json_string);
echo $obj->name;
echo $obj->office[0]; //displays google

Output :-







Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.