Php json processing functions

Source: Internet
Author: User

JavaScript Object Notation is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. PHP-based serialize is used to serialize variables and return a string expression with the variable type and structure, <br/> both represent a Data Structure in a string. What is the difference between them. <Br/> Start With JSON and look at a simple instance. <Br/> Example 1: <br/> A Collection of name/value pairs (pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ). <Br/> An ordered list of values ). In most languages, it is understood as an array ). <Br/> var test = {"name": "Peter", "Age": 20 };< br/> document. write (test. name + ":" + test. age); <br/> display result: <br/> Peter: 20 <br/> variable {"name": "Peter", "Age ": 20} is an object with two elements (like an array of PHP): <br/> name: Peter, age: 20. <Br/> of course, it can also become more complex. <Br/> Example 2: <br/> var test = {"user": {"name": "Peter", "Age": 20}, "company ": "Ford" };< br/> document. write (test. user. name + ":" + test. company); <br/> display result: <br/> Peter: Ford <br/> in this example, the user element contains the name and age. <Br/> If You Want To embody multiple users, you need to use an array, which is different from the object's "{}", and the array uses "[]". <Br/> Example 3: <br/> var test = [<br/> {"user": {"name": "Peter", "Age": 20 }, "Company": "Ford" },< br/> {"user": {"name": "Li Ming", "Age": 20}, "company ": "Benz"} <br/>]; <br/> document. write (test [1]. user. name + ":" + test [1]. company); <br/> // or use: document. write (test [1] ["user"] ["name"] + ":" + test [1] ["company"]); <br/> display result: <br/> Li Ming: Benz <br/> the preceding simple instance can transmit some complex data through a single string. It is indeed much more convenient to use Ajax together. <Br/> let's take a look at the function serialize of PHP. <Br/> Example 4: <br/> $ arr = array <br/> (<br/> 'Peter '=> array <br/> (<br/> 'country' => 'usa', <br/> 'age' => 20 <br/> ), <br/> 'Li ming' => array <br/> (<br/> 'country' => 'China ', <br/> 'age' => 21 <br/>); <br/> $ serialize_var = serialize ($ ARR ); <br/> echo $ serialize_var; <br/> result: <br/> A: 2: {s: 5: "Peter"; A: 2: {s: 7: "country"; s: 3: "USA"; s: 3: "Age"; I: 20;} s: 7: "Li Ming"; A: 2: {s: 7: "country"; s: 5: "China"; s: 3: "ag E "; I: 21 ;}}< br/> This result looks more complex than JSON, but it is also very simple. It describes some data types and structures. <Br/> take a: 2: {s: 7: "country"; s: 3: "USA"; s: 3: "Age"; I: 20 ;} for example: <br/> A: 2 indicates that this is an array (array) with two elements. <br/> S: 7: "country"; s: 3: "USA"; is the first element. S: 7 indicates that this is a string with 7 characters. <br/> I: 20; it should also be guessed to be an integer (integer) 20. <Br/> let's take a look at this example. <br/> Example 5: <br/> class test <br/>{< br/> var $ Var = 0; <br/> function add () {<br/> echo $ var + 10; <br/>}< br/> $ unserialize_var = new test; <br/> $ serialize_var = serialize ($ unserialize_var); <br/> echo $ serialize_var; <br/> $ unserialize_var = NULL; <br/> $ unserialize_var = unserialize ($ serialize_var); <br/> $ unserialize_var-> Add (); <br/> display result: <br/> O: 4: "test": 1: {s: 3: "Var"; I: 0 ;}< br/> 10 <br/> in this example, we can see that serialize stores the data type and structure. <br/> the variable after unserialize can still use add () method. <Br/> are there any contacts between PHP and JSON? If you are familiar with PHP, you should know that php5.2.0 has set JSON extension as the default component, that is, you can perform JSON operations in PHP, the functions are json_encode and json_decode. <Br/> Example 6: <br/> $ arr = array <br/> (<br/> 'name' => 'Peter ', <br/> 'age' => 20 <br/>); <br/> $ jsonencode = json_encode ($ ARR); <br/> echo $ jsonencode; <br/> display result: <br/> {"name": "Peter", "Age ": 20} <br/> the result is the same as the test value in the example. The variable in PHP is converted to a JSON character expression through json_encode. <Br/> let's take a look at the json_decode usage. <Br/> Example 7: <br/> $ Var = '{"name": "Peter", "Age": 20 }'; <br/> $ jsondecode = json_decode ($ var); <br/> print_r ($ jsondecode); <br/> display result: <br/> stdclass object ([name] => Peter [age] => 20) <br/> This is indeed verified, in JSON {"name ": "Peter", "Age": 20} is an object, but it can also be converted into an array in PHP. Set the assoc parameter to true in json_decode. <Br/> Example 8: <br/> $ Var = '{"name": "Peter", "Age": 20 }'; <br/> $ jsondecode = json_decode ($ var, true); <br/> print_r ($ jsondecode); <br/> display result: <br/> array ([name] => Peter [age] => 20) <br/> note that JSON is in unicode format, so to perform a Chinese operation to convert it to the UTF-8 format. <Br/> I believe you have a preliminary understanding of JSON and PHP serialize and json_encode through the above examples, <br/> combined with PHP, JavaScript, JSON, and Ajax, you can complete powerful data interaction functions. 

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.