In-depth analysis of various problems with JSON Functions

Source: Internet
Author: User
Tags format definition

Our basic solution is to transcode Chinese on the server side before the conversion, such as the JSON function or the hexadecimal conversion, and then convert the client accordingly, we still need to find a faster way, or it is best to use the JSON extension of PHP to directly support Chinese characters.

Today, I tested the built-in JSON function in PHP 5.2. JSON is mainly used for the data exchange format during Remote Access in Ajax, it can be said that it is a lightweight data exchange format used to replace the XML format in Ajax. The potential is not to be underestimated, because Google Maps is a typical example of using JSON without XML.

  • Describes in detail the conversion of JSON serialization.
  • The author provides detailed instructions on the Json getting started document.
  • A full description of the JSON history
  • Comparison between JSON and XML
  • Details about the number of elements in JSON format that the server code will generate

For more information about JSON, see here for more articles. For more information about format definition, refer to the official JSON website. For more information about JSON, see Google.

 
 
  1. <?Php 
  2. // Object
  3. Class JsonTest {
  4. Var $Id=1;
  5. Var $Name='Heiyeluren';
  6. Var $Gender=Male';
  7. }
  8. $Obj=NewJsonTest;
  9. Echo json_encode ($ obj )."<Br />";
  10.  
  11. // Numeric index array
  12. $Arr1=Array(1, 'heiyeluren', 'male ');
  13. Echo json_encode ($ arr1 )."<Br />";
  14.  
  15. // Associate the Index Array
  16. $Arr2=Array("Id" =>1, "name" =>'Heiyeluren', "gender" =>Male ');
  17. Echo json_encode ($ arr2 )."<Br />";
  18.  
  19. // Multi-dimensional numeric index array
  20. $Arr3=Array(Array (1, 'heiyeluren', 'male'), array (1, 'heiyeluren', 'male '));
  21. Echo json_encode ($ arr3 )."<Br />";
  22.  
  23. // Associate the Index Array
  24. $Arr4=Array(Array ("id" =>1, "name" =>'Heiyeluren', "gender" =>'Male'), array ("id" =>1, "name" =>'Heiyeluren', "gender" =>Male '));
  25. Echo json_encode ($ arr4 )."<Br />";
  26. ?> 
  27.  
  28.  
  29.  
  30. Browser output:
  31.  
  32.  
  33. {"Id": 1, "name": "heiyeluren", "gender ":""}
  34. [1, "heiyeluren", ""]
  35. {"Id": 1, "name": "heiyeluren", "gender ":""}
  36. [[1, "heiyeluren", ""], [1, "heiyeluren", ""]
  37. [{"Id": 1, "name": "heiyeluren", "gender": "" },{ "id": 1, "name": "heiyeluren ", "gender": ""}]

According to the JSON function definition, we can find the following features:

1. Pure Digital indexes are stored in arrays recognized by JavaScript.
2. The associated index array is constructed in the form of a JavaScript Object.
3. Multi-dimensional associated indexes are JavaScript arrays in the periphery, and the index arrays in the middle are objects.
4. Chinese cannot be recognized. All Chinese strings are displayed as blank

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.