JSON application method in PHP

Source: Internet
Author: User
How to use JSON, PHP52 has built-in support for JSON. of course, if it is earlier than this version, there are many PHP versions available on the market, and it will be OK for any next use, now we mainly talk about the built-in JSON supported by PHP, which is very simple. There are two functions: json_encode and json_decode.

JSON application method in PHP

How to use JSON, PHP5.2 began to have built-in support for JSON. of course, if it is earlier than this version, there are many PHP versions available on the market, and it will be OK as soon as it is used, now I am mainly talking about the built-in JSON supported by PHP. it is very simple. There are two functions: json_encode and json_decode (similar to serialization), one encoding and one Decoding. let's first look at the encoding usage:

  1. $ Arr = array (
  2. 'Name' => 'Chen yixin ',
  3. 'Nick '=> 'deep ary ',
  4. 'Contact '=> array (
  5. 'Email '=> 'shenkong at qq dot com ',
  6. 'Website' => 'http: // www.phpfensi.com ',
  7. )
  8. );
  9. $ Json_string = json_encode ($ arr );
  10. Echo $ json_string;
  11. ?>

Very simple will be an array of JSON, it should be noted that, in non-UTF-8 encoding, Chinese characters will not be encode, the results will be null, so, if you use gb2312 to write PHP code, you need to use iconv or mb to convert the content containing Chinese characters into a UTF-8 and then perform json_encode. the above output is as follows:

  1. {"Name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com", "website ": "http://www.phpfensi.com "}}

I have mentioned that it is similar to Serialization. if you do not believe it, decoding is required after Encoding. PHP provides the corresponding function json_decode. after json_decode is executed, an object will be obtained. The operation is as follows:

  1. $ Arr = array (
  2. 'Name' => 'Chen yixin ',
  3. 'Nick '=> 'deep ary ',
  4. 'Contact '=> array (
  5. 'Email '=> 'shenkong at qq dot com ',
  6. 'Website' => 'http: // www.phpfensi.com ',
  7. )
  8. );
  9. $ Json_string = json_encode ($ arr );
  10. $ Obj = json_decode ($ json_string );
  11. Print_r ($ obj );
  12. ?>

Will access the attributes in the object? $ Obj-> name. of course, you can also index the array to facilitate the call:

  1. $ Json_string = json_encode ($ arr );
  2. $ Obj = json_decode ($ json_string );
  3. $ Arr = (array) $ obj;
  4. Print_r ($ arr );

PHP transfer and transfer are not very useful. in addition to cache generation, it feels better to store arrays directly. However, when you interact with the front-end, the function will come out, next let's take a look at how I use Javascript to use this character:

  1. Var arr = {"name": "u9648u6bc5u946b", "nick": "u6df1u7a7a", "contact": {"email": "shenkong at qq dot com ", "website": "http://www.phpfensi.com "}};
  2. Alert (arr. name)
  3. Script

In the above, the string is directly assigned to a variable, and it becomes a Javascript array (specialized terms should not be called arrays, but due to the habits of PHP, I have always called the array for ease of understanding). In this way, you can easily traverse arr or do whatever you want to do. it seems that AJAX is not mentioned here? Yes. in retrospect, if the server returns responseText with a JSON string instead of XML, will the front-end Javascript processing be very convenient? Dog Skin plaster is used in this way.

As a matter of fact, apart from the different storage formats of data, there is no big difference between JSON and XML. However, as I mentioned below, although it has little to do with XML, it can be explained that a larger range of applications in JSON, that is, cross-origin data calling, AJAX does not support cross-origin calling due to security issues, so it is very troublesome to call data under different domain names, although there is a solution (stone mentioned proxy in his lecture), I can write two files to demonstrate cross-origin calls.

Access key file index.html

  1. Function getProfile (str ){
  2. Var arr = str;
  3. Document. getElementById ('Nick '). innerHTML = arr. nick;
  4. }
  5. Script
  6. "Nick">
  7. Script

Profile. php

  1. $ Arr = array (
  2. 'Name' => 'Chen yixin ',
  3. 'Nick '=> 'deep ary ',
  4. 'Contact '=> array (
  5. 'Email '=> 'shenkong at qq dot com ',
  6. 'Website' => 'http: // www.phpfensi.com ',
  7. )
  8. );
  9. $ Json_string = json_encode ($ arr );
  10. Echo "getProfile ($ json_string )";
  11. ?>

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.