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:
- $filepath = $_SERVER['DOCUMENT_ROOT'];
- include $filepath.'/inc/json/Encoder.PHP';
- $encoder = new Zend_Json_Encoder(false);
- $json = $encoder ->encode($result);
- 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.