JSON extension is a part of PHP 5.2.0.
JSON functions that are available in PHP.
- json_decode() : Decodes a JSON string
- json_encode() : Returns the JSON representation of a value
- json_last_error_msg() : Returns the error string of the last json_encode() or json_decode() call
- json_last_error() : Returns the last error occurred
PHP array to JSON Object :
<?php
//PHP array
$phpArray = array (
'songName' => 'Hotel California',
'artistName' =>'Eagles'
);
//PHP array converted to json Object using json_encode()
echo json_encode($phpArray);
?>
Output: {"songName":"Hotel California","artistName":"Eagles"}
PHP Object to JSON Object :<?php
//PHP Class
class Song {
public $songName ="";
public $songArtist ="";
}
//PHP object
$mySong = new Song();
$mySong ->songName = "November rain";
$mySong -> songArtist = "Guns and Roses";
//JSON object
$jsonObj = json_encode($mySong);
echo $jsonObj;
?>
Output: {"songName":"November rain","songArtist":"Guns and Roses"}
PHP JSON encode function Examples :JSON Object to PHP Array :
<?php
//JSON Object
$jsonMySong = '{"songName":"November rain",
"songArtist":"Guns and Roses"}';
//PHP Array
$phpArrayObj = json_decode($jsonMySong,true);
var_dump($phpArrayObj);
?>
Output: array(2) {
["songName"]=> string(13) "November rain"
["songArtist"]=> string(14) "Guns and Roses"
}
<?php
//JSON Object
$jsonMySong = '{"songName":"November rain",
"songArtist":"Guns and Roses"}';
//PHP Object
$phpyObj = json_decode($jsonMySong);
var_dump($phpObj);
?>
Output: object(stdClass)#1 (2) {
["songName"]=> string(13) "November rain"
["songArtist"]=> string(14) "Guns and Roses"
}
More Posts related to Json-Tutorial,
- JSON Schema Validator Libraries: JSON Tutorial
- JSON Syntax : JSON Tutorial
- JSON Tutorial: List of Lessons
- JSON Nest Objects Example: JSON Tutorial
- JSON Text to JavaScript Object using eval() Example: JSON Tutorial
- JSON Datatypes : Tutorial
- JSON with PHP Example: JSON Tutorial
- JSON Schema and Hyper-Schema : JSON Tutorial
More Posts:
- Android Images with Rounded Corners : ImageView - Android
- Save webpage as pdf in Google Chrome for Mac OS X - Mac-OS-X
- Change SharePoint search results FullTextSqlQuery RowLimit 10000 - SharePoint
- SharePoint Excel error - The workbook cannot be opened because it contains the following features that are not supported by Excel in the browser - SharePoint
- FileZilla Connection time out Failed to retrieve directory listing - FTP
- Tutorial Java SOAP WebServices JAS-WS with Eclipse J2EE IDE and Tomcat Server Part 1 - Java
- Android RatingBar Example - Android
- How to use Content Assist in Eclipse IDE - Eclipse
- List of Java Keywords - Java
- [Solutions] Android Error in an XML file: aborting build. Eclipse SDK - Android
- How to configure PDF iFilter for SharePoint - SharePoint
- Create simple struts2 project using maven commands - Java
- Change Height of Android ActionBar - Android
- How to enable disable SharePoint Developer Dashboard for tracing troubleshooting - SharePoint
- Eclipse: Updating Maven Project. Unsupported IClasspathEntry kind=4 - Eclipse