TIPS: Use php json extension correctly

Source: Internet
Author: User
Tags php database php json

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 ()

 
 
  1. < ?php  
  2. $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}}';  
  3. $decodedObject = json_decode($jsonObject);  
  4. $decodedArray = json_decode($jsonObject, true);  
  5. print_r($decodedObject);  
  6. echo "< br>< br>";  
  7. print_r($decodedArray);  
  8. ?>   
  9.  

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:

 
 
  1. 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] => ) )   
  2.  

Let's take a look at encode:

 
 
  1. < ?php  
  2. $results = array("21" =>   
  3. array("url" =>   
  4. "www.blah.com/story1.html", "title" =>   
  5. "JSON is sweeping AJAX world", "viewed" =>   
  6. FALSE), "22" => array("url"=>   
  7. "www.blah.com/story2.html", "title" =>   
  8. "JSON is great", "viewed" => FALSE));  
  9. $jsonObject = json_encode($results);  
  10. echo $jsonObject;  
  11. ?>   
  12.  

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.
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.