Solution for PHP to return NULL when processing Json string decoding

Source: Internet
Author: User
This article mainly introduces how PHP can handle Json string decoding and return NULL. it uses an example of a filter function to demonstrate how to handle json return errors, and lists the common causes of this error. if you need a friend, refer to the example in this article to describe how PHP processes Json string decoding and returns NULL. Share it with you for your reference. The specific method is as follows:

In general, php uses the json_decode () function to decode json strings. if the first parameter is set to true, an array is returned. if the second parameter is set to false, an object is returned. If NULL is returned, an error is returned. the json_last_error () is output, and the error message corresponding to the obtained integer is displayed. As shown in:

Json_last_error () is a common integer 4, which is a syntax error because the json string is incomplete before json_decode.

Therefore, some characters submitted by the client may affect the json format. JS can be used for filtering to solve general problems, mainly filtering carriage return, spaces, and html tags.

The implementation code is as follows:

/** Filter function */function htmlEncode (str) {str = str. replace (/\ s +/ig, ''); str = str. replace (// g, ''); str = str. replace (//G, ''); str = str. replace (/(? : T | v | r) * n/g ,'
'); Str = str. replace (/t/g, ''); str = str. replace (/x22/g, '"'); str = str. replace (/x27/g, '''); str = str. replace (/"/g," "); return str ;}

In this case, you must submit the json string data to the server for processing and filter it only on the client.

Other causes for returning NULL in json_decode ($ str:

1. $ str can only be UTF-8 encoded

2. the element cannot end with a comma (different from the php array)

3. elements cannot use single quotes

4. element values cannot have spaces and must be replaced.

If you have encountered the above situation, you can follow the above methods. I hope this article will help you with PHP programming.

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.