The application of JSON in PHP

Source: Internet
Author: User

The internet today, Ajax is not a strange word. Speaking of Ajax, you may immediately recall the XML that arose as a result of RSS. XML parsing, I am afraid, is no longer a problem, especially PHP5, the emergence of a large number of XML parsers, such as the most lightweight simplexml. However, for Ajax, XML parsing is more inclined to the support of the foreground javascrīpt. I think all the people who have parsed the XML will have a big head because of the tree and the nodes. Admittedly, XML is a very good way to store data, but its flexibility precisely causes the difficulty of its parsing. Of course, the difficulty referred to here is relative to the protagonist--json in this article.

What is JSON? I will not repeat the concept. In layman's parlance, it is a storage format for data, just like a string after a PHP serialization. It is a data description. For example, after we have serialized an array and stored it, we can easily deserialize it and then apply it. The same is true of JSON, except that he builds an interactive bridge between client javascrīpt and server PHP. We use PHP to generate the JSON string, and then pass this string to the foreground javascrīpt,javascirpt can easily reverse JSON and then apply it. Speaking of popular point, it really looks like an array.

Anyway, how to use JSON. PHP5.2 began with the built-in JSON support. Of course, if lower than this version, then there are many PHP version of the implementation, just the next use on the OK. Now the main is to talk about PHP built-in support for JSON. Very simple, two functions: Json_encode and Json_decode (like serialization). One encoding, one decoding. First look at the use of the code:

<?php
$arr = array(
  'name' => '陈毅鑫',
  'nick' => '深空',
  'contact' => array(
    'email' => 'shenkong at qq dot com',
    'website' => 'http://www.devdao.com',
  )
);
$json_string = json_encode($arr);
echo $json_string;
?>

It's very simple to have an array of JSON. It should be pointed out that, in the UTF-8 encoding, the Chinese characters will not be encode, the result will come out null value, so if you use gb2312 to write PHP code, then you need to use the content of English to iconv or MB to UTF-8 and then Json_encode, The results of the above output are as follows:

{"name":"\u9648\u6bc5\u946b","nick":"\u6df1\u7a7a","contact":{"email":"shenkong at qq dot com","website":"http:\/\/www.chenyixin.com"}

I said it's like serialization, and you don't believe it. After encoding to decode, PHP provides the corresponding function Json_decode,json_decode execution, will get an object, the operation is as follows:

<?php
$arr = array(
  'name' => '陈毅鑫',
  'nick' => '深空',
  'contact' => array(
    'email' => 'shenkong at qq dot com',
    'website' => 'http://www.devdao.com',
  )
);
$json_string = json_encode($arr);
$obj = json_decode($json_string);
print_r($obj);
?>

Access to properties within an object, right? $obj->name, this way, of course, you can also turn it to an array for easy invocation:

$json_string = json_encode($arr);
$obj = json_decode($json_string);
$arr = (array) $obj;
print_r($arr);

The use of PHP is not particularly large, in addition to cache generation, feel better than directly to save the array, but when you and the front of the interaction, its role is out, below see how I use JAVASCRĪPT to use this paragraph of characters:

<script type="text/javascript">
var arr = {"name":"\u9648\u6bc5\u946b","nick":"\u6df1\u7a7a","contact":{"email":"shenkong at qq dot com","website":"http:\/\/www.chenyixin.com"}};
alert(arr.name)
</script>

Above, the string is directly assigned to a variable, and it becomes a javascrīpt array (the specialization term should not be called an array, but because of PHP's customary problems, I've been called an array well, easy to understand). This way, you can easily traverse the arr or do whatever you want to do. Writing here, it seems to have not mentioned Ajax Oh? Yes, well, if the server-side returns responsetext with a JSON-like string instead of XML, is the front desk javascrīpt easy to handle? That's how Dogskin plaster is used.

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.