JSON is a protocol designed to allow middleware to create objects in the inherent JavaScript format. Its most powerful property is that it is a lightweight protocol. When you simply process RSS aggregation or recipe lists, you do not need to use all the XML functions in JavaScript. You do not need to verify the format or make sure that the data is strictly typed.
Php json extension encoding and decoding
There are two functions for php json Extension: encode and decode. The first function converts any type of data objects into a group of serialized data for JavaScript processing. The second function decodes the serialized data and converts it to a basic PHP object or a union array. Let's take a look at json_decode ().
Example of json_decode ()
- < ?php
- $jsonObject = '{"21":{"url":"www.blah.com
/story1.html","title":"JSON is sweeping
AJAX world","viewed":false},"22":{"url":
"www.blah.com/story2.html","title":"JSON
is great","viewed":false}}';
- $decodedObject = json_decode($jsonObject);
- $decodedArray = json_decode($jsonObject, true);
- print_r($decodedObject);
- echo "< br>< br>";
- print_r($decodedArray);
- ?>
-
As shown above, we have a PHP script that retrieves $ jsonObject and decodes it back to the inherent PHP Object. We decoded it twice. For the first time, use the unmodified usage to obtain the stdClass object. For the second time, use the Boolean parameter to create the Union array.
The output of decode is as follows:
- stdClass Object ( [21] =>
stdClass Object ( [url] =>
www.blah.com/story1.html [title] =>
JSON is sweeping AJAX world [viewed] =>
) [22] => stdClass Object ( [url] =>
www.blah.com/story2.html [title] =>
JSON is great [viewed] => ) ) Array ( [21] =>
Array ( [url] => www.blah.com/story1.html
[title] => JSON is sweeping AJAX world
[viewed] => ) [22] => Array ( [url] =>
www.blah.com/story2.html [title] =>
JSON is great [viewed] => ) )
-
Let's take a look at encode:
- < ?php
- $results = array("21" =>
- array("url" =>
- "www.blah.com/story1.html", "title" =>
- "JSON is sweeping AJAX world", "viewed" =>
- FALSE), "22" => array("url"=>
- "www.blah.com/story2.html", "title" =>
- "JSON is great", "viewed" => FALSE));
- $jsonObject = json_encode($results);
- echo $jsonObject;
- ?>
-
No recursion is used. No tag is added. You only need to pass it into the json_encode () function, and then it will be passed out from the other end as a JSON serialized object.
Conclusion
JSON is a useful and lightweight protocol that can now be used in PHP V5.2. It can easily extract data from PHP applications and put it into Ajax applications. Correspondingly, php json extensions are also lightweight and useful, and only contain two easy-to-use functions.
Using these functions, we can convert and export the object structure, and use json_encode () to make the data connected to the PHP database available for Ajax applications. After processing data in the Ajax application, you can return the data to the PHP script and recreate the available Object Data Structure with json_decode. After the data is returned to PHP, we can store it in the database, or use any other data processing methods provided by PHP.