How to correctly solve the problem of missing JSON package in PHP_PHP Tutorial

WBOY
Release: 2016-07-15 13:32:47
Original
950 people have browsed it

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

The solution to the lack of JSON package in these two PHPs is mainly to convert arrays, strings, or objects into json format data (a special structure format). The first thing you need is the json_encode method. This method mainly converts arrays, strings, objects, etc. into json format. Method to solve this problem:

a. Simple version:

Directly according to your own problem needs (you only need to operate a simple array), you can directly This array is concatenated.

b. Perfect encode method:

A simple way to solve the problem of PHP's lack of JSON package is to use open source code completed by others:

First think of zend framework. Get Decoder.PHP, and Encoder.PHP from the zf_json package. Decoder.PHP actually only needs to remove the protect of one of the protect methods. I forgot the specific operation, readers can debug it by themselves to solve it. After rewriting the Decoder class, call it as follows:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">filepath</span><span> = $_SERVER['DOCUMENT_ROOT'];  </span></span></li>
<li>
<span>include $filepath.'/inc/json/Encoder.</span>PHP<span>';   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">encoder</span><span> = </span><span class="attribute-value">new</span><span> Zend_Json_Encoder(false);  </span>
</li>
<li>
<span>$</span><span class="attribute">json</span><span> = $encoder -</span><span class="tag">></span><span>encode($result);   </span>
</li>
<li class="alt"><span>header("X-JSON: $json"); </span></li>
</ol>
Copy after login

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

Since many places where json is used are still in ajax calls, some ajax 'frameworks' provide decode methods to solve the problem of PHP lacking JSON packages:

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

In addition to the problem of PHP missing the JSON package, you may encounter problems with encoding.

The Chinese character encoding returned by zend framework probably does not correspond to my page encoding (utf-8) (not that it is not utf-8), so there will be problems. Therefore, it will have an impact on the error messages (Chinese characters) returned by some verifications. The alternative here is to use the traditional interface to return a numeric code form (a method often used by communication gateways). For example, returning json.code has many possibilities such as 0, 1, 2, 3, etc. When processing on the js side, 0 indicates success, 1 indicates illegal client data, and so on. The problem was solved perfectly.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446104.htmlTechArticleThe alternative to this problem is to override the json_decode and json_encode methods of PHP json. The solution to the lack of JSON packages in these two PHPs is mainly to convert arrays, strings, or objects...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!