Tips to summarize correct use of PHP JSON extension _php tutorial

Source: Internet
Author: User
Tags php database php json

JSON is a protocol designed to allow middleware to create objects that use the native JavaScript format. Its most powerful attribute is that it is a lightweight protocol. When you simply work with RSS aggregation or recipe lists, you don't need to use the full functionality of XML in JavaScript. You do not need to validate the format or ensure strict data typing.

PHP JSON extension encoding and decoding

There are two functions for PHP JSON extensions: Encode and decode. The first function converts any type of data object into a set of serialized data for JavaScript processing. The second function decodes the serialized data and converts it to a basic PHP object or union array. Let's take a look at Json_decode ().
Example of Json_decode ()

 
 
  1. < ? PHP
  2. $ Jsonobject = ' {' + ': {' url ': ' Www.blah.com
    /story1.html "," title ":" JSON is sweeping
    AJAX World "," viewed ": false}," $ ": {" 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. ?>

As above, we have a PHP script that takes $jsonObject and decodes it back into PHP's intrinsic object. We have decoded two times. For the first time, using unmodified usage, this will get the StdClass object, and the second time, use the Boolean parameter to create the Union array.
The following is the output of the decode:

 
 
  1. StdClass Object ([+] =>
    StdClass Object ([url] =>
    www.blah.com/story1.html [title] =>
    JSON is sweeping AJAX world [viewed] =>
    ) [] =>stdClass Object ([url] =>
    www.blah.com/story2.html [title] =>
    JSON is great [viewed] =>) ) Array ([+] =>
    Array ([url] =>www.blah.com/story1.html
    [Title] =>JSON is sweeping AJAX world
    [Viewed] =>) [] =>Array ([url] =>
    www.blah.com/story2.html [title] =>
    JSON is great [viewed] => ) )

Let's look at encode again:

 
 
  1. < ? PHP
  2. $ Results = Array ("+" =>
  3. Array ("url" =>
  4. " www.blah.com/story1.html", "title" = >
  5. "JSON is sweeping AJAX world", "viewed" = >
  6. FALSE), "$" => array ("url" =>
  7. " www.blah.com/story2.html", "title" = >
  8. "JSON is great", "viewed" = > FALSE));
  9. $ Jsonobject = Json_encode ($results);
  10. Echo $jsonObject;
  11. ?>

Recursion is not used. No markup was added. Simply pass it in to the Json_encode () function, and it will be outgoing from the other end as a JSON serialized object.

Conclusion

JSON is a useful, lightweight protocol that can now be used in PHP V5.2, which makes it easy to extract data from a PHP application and put it into an Ajax application. Accordingly, the PHP JSON extension is also lightweight and useful, with only two easy-to-use functions.

Using these functions, we can convert and export object structures, and use Json_encode () to make data from a PHP database connection available to Ajax applications. After processing the data in an Ajax application, you can return the data to the PHP script and recreate the available object data structures with Json_decode (). When the data is returned to PHP, we can store it in a database or use any of the other data processing methods available in PHP for many choices.


http://www.bkjia.com/PHPjc/446097.html www.bkjia.com true http://www.bkjia.com/PHPjc/446097.html techarticle JSON is a protocol designed to allow middleware to create objects that use the native JavaScript format. Its most powerful attribute is that it is a lightweight protocol. Simple processing RSS aggregation or recipe columns ...

  • 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.