PHP handling JSON string decoding returns null workaround, jsonnull_php tutorial

Source: Internet
Author: User
Tags php programming

PHP handles JSON string decoding to return null resolution, Jsonnull


This article explains how PHP handles JSON string decoding to return null. Share it for everyone's reference. Here's how:

In general,PHP uses the Json_decode () function for JSON string decoding, the first argument to pass a string, and the second parameter, if true, to return an array, or false to return an object. If NULL is returned, indicating an error, the output json_last_error (), the resulting integer value corresponding to the wrong hint . As shown in the following:

Json_last_error () is more common than the integer 4, which is a JSON string that was incomplete before Json_decode, so syntax error.

Then must be the client to submit the individual characters affect the format of the JSON, you can use JS to filter, you can solve the general problem, mainly filter returns, spaces, 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-side processing, which can only be filtered by the client.

Other Json_decode ($STR) return null for some reasons:

1. $STR can only UTF-8 encoding

2. The element cannot end with a comma (unlike PHP's array)

3. Elements cannot use single quotation marks

4. There must be no spaces and \ n in the middle of the element value

If you encounter the above situation, you can follow the above method to deal with. I hope this article is helpful to everyone's PHP programming.


PHP receives the JSON returned by PHP, the parsed value is null

B.php must submit a cookie value when fetching JSON content, otherwise a.php will only return empty content.

PHP array is converted to JSON, and the string "Restaurant/café" is turned into JSON after it is displayed as null how can I display the string correctly

You can try it by using single quotation marks to get your keys.

$arr = Array (' Amenities ' =>array (' restaurant/café ' = = Array (' classroom ' = ' classroom ')));
echo Json_encode ($arr);

http://www.bkjia.com/PHPjc/872372.html www.bkjia.com true http://www.bkjia.com/PHPjc/872372.html techarticle PHP handles JSON string decoding to return null resolution, Jsonnull This article describes the PHP processing JSON string decoding return NULL resolution. Share it for everyone's reference. ...

  • 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.