What is JSON?
JSON (JavaScript Object Notation) is a lightweight format for serializing data when passing it between systems that do not natively support complex data types. JSON has become the most popular format for information exchange on the web — web services send and receive serialized JSON data constantly.
JSON is designed for JavaScript but is language-agnostic. PHP 5.2+ ships with a built-in JSON extension, giving you json_encode() and json_decode() out of the box.
json_encode()
Converts a PHP value (string, number, array, object) into its JSON string representation.
json_decode()
Converts a JSON string back into a PHP value (by default a stdClass object, or an array when you pass true as the second argument).
Example
The JSON process has two parts. First we encode a PHP array, then we decode it and use the data.
Part 1 — Encoding
<?php
$data = array(
'php' => 3,
'code' => 4,
'beginner' => 22,
'java' => array("javascript", "jquery")
);
echo json_encode($data);
// Output: {"php":3,"code":4,"beginner":22,"java":["javascript","jquery"]}
?>
Part 2 — Decoding
<?php
$json_string = '{"php":3,"code":4,"beginner":22,"java":["javascript","jquery"]}';
$object = json_decode($json_string);
echo $object->php; // outputs: 3
echo $object->java[0]; // outputs: javascript
?>
Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.