Solution for PHP to return NULL when processing Json string decoding, jsonnull_PHP tutorial-php Tutorial

Source: Internet
Author: User
Solution for PHP to return NULL when processing Json string decoding, jsonnull. PHP solves the problem of returning NULL when processing Json string decoding. jsonnull this article describes how PHP can solve the problem of returning NULL when processing Json string decoding. Share it with you for your reference. Solution for PHP to return NULL when processing Json string decoding, jsonnull

This example describes how PHP can process the return value of NULL for Json string decoding. 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. the element value cannot be empty or \ n in the middle. it 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.


Php receives json returned by php. the parsed value is NULL.

B. php must submit the cookie value when retrieving json content; otherwise, a. php will only return null content.

The PHP array is converted to JSON, and the string "Restaurant/café" is converted to json, and then displayed as null. how can I correctly display the string?

Put your key in single quotes and try again.

$ Arr = array ('amenities '=> array ('restaurant/café' => array ('classroom' => 'classroom ')));
Echo json_encode ($ arr );

Examples in this article describes how PHP can process the return value of NULL for Json string decoding. Share it with you for your reference. With...

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.