How to solve PHP's lack of JSON packages correctly _ PHP Tutorial

Source: Internet
Author: User
Tags php json
How to solve PHP's lack of JSON packages correctly. The alternative to this problem is to rewrite the json_decode and json_encode methods of PHPjson. The solution to the lack of JSON packages for these two PHP files is mainly to convert arrays, strings, or objects.

The alternative to this problem is to rewrite the json_decode and json_encode methods of PHP json.

The solution to the lack of JSON packages for these two PHP files is mainly to convert arrays, strings, or objects into json data (a special structure format ). The json_encode method is required first. this method converts arrays, strings, and objects into json format. Solution:

A. simple edition:

Directly address your own problems (you only need to operate a simple array), you can directly splice this array.

B. Complete encode method:

The simple solution to PHP's lack of JSON package is to use the open source code completed by others:

First, consider zend framework. Use Decoder. PHP and Encoder. PHP in the zf_json package. Decoder. PHP only needs to remove the protect of a protect method. If you forget the specific operation, you can debug it yourself. After the Decoder class is changed, the call is as follows:

 
 
  1. $filepath = $_SERVER['DOCUMENT_ROOT'];
  2. include $filepath.'/inc/json/Encoder.PHP';
  3. $encoder = new Zend_Json_Encoder(false);
  4. $json = $encoder ->encode($result);
  5. header("X-JSON: $json");

The json_decode method is the same as above, if it is used by PHP.

Because json is still used in ajax calls, some ajax 'framework' provides the decode method to solve the problem of PHP's lack of JSON packages:

For example, in prototype, if the PHP page contains prototype. js, you can directly call function (transport, json ){.. Operation} The json data is json. a, json. B, and so on.

In addition to the absence of JSON packages in PHP, encoding may occur.

The Chinese character encoding returned by zend framework does not correspond much (not to say it is not UTF-8) to my page encoding (UTF-8), so there will be problems. Therefore, some error messages (Chinese characters) returned for verification may be affected. Here, the alternative method is to use the traditional interface to return the digital code form (the communication gateway often uses the method), for example, the returned json. code may be 0, 1, 2, 3, and many other possibilities. When processing on the js end, 0 indicates success, 1 indicates invalid client data, and so on. The problem is solved perfectly.


Jsonjson's json_decode and json_encode methods. The solution to the lack of JSON packages for these two PHP files is mainly to convert arrays, strings, or objects...

Related Article

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.