Some strings in PHP that cannot be parsed by json_decode

Source: Internet
Author: User

Some unresolvable strings in PHP of json_decode include the following common types.

I. Bug #42186 json_decode () won't work with \ L

When the string contains \ L, json_decode cannot be parsed. Test code:

echo "***********json_decode() won't work with \l*************<br/>";$json = '{"stringwithbreak":"line with a \lbreak!"}';var_dump($json);//stringwithbreak":"line with a \lbreak!var_dump(json_decode($json, true));//null

Solution:

It is mainly to replace \ L. Of course, if '\ l' is really needed, we must not use json_decode for parsing and commit as a character.

var_dump(str_replace("\\l", "", $json));//stringwithbreak":"line with a break!print_r(json_decode(str_replace("\\l", "", $json), true));//Array ( [stringwithbreak] => line with a break! ) 

Ii. tabs in Javascript Strings Break json_decode ()

When the string contains the tab key, json_decode () cannot be parsed, for example, code 3-1.

echo "<br/>***********Tabs in Javascript strings break json_decode()*************<br/>";var_dump(json_decode('{ "abc": 12, "foo": "barbar" }'));

The returned result after execution is null.

Solution:

1. When encountering a string containing the tab key input, we should avoid uploading data to PhP using JSON, and then use PHP for parsing.

2. You can also use the following 3-2 code to replace

$myStr = '{ "abc": 12, "foo": "barbar" }';$replaceStr = str_replace("", "\\t", $myStr);var_dump($replaceStr);var_dump(json_decode($replaceStr ));

3. json_decode returns false when leading zeros aren't escaped with double quotes

If the value of JSON is of the number type and the number starts with 0, for example, code 4-1

echo "<br/>***********json_decode returns false when leading zeros aren't escaped with double quotes*************<br/>";$noZeroNumber = '{  "test" : 6}';$zeroNumber= '{  "test" : 06}';var_dump(json_decode($noZeroNumber));//object(stdClass)[1]  public 'test' => int 6var_dump(json_decode($zeroNumber));//null

This problem may rarely occur, but once it occurs, it is difficult to find the cause of the problem.

4. Decode chokes on unquoted object keys

If the key value is not enclosed in quotation marks, it cannot be parsed, for example, Code 5-1.

echo "<br/>***********decode chokes on unquoted object keys*************<br/>";var_dump(json_decode('{"a":"tan","model":"sedan"}'));//object(stdClass)[1]  public 'a' => string 'tan' (length=3)  public 'model' => string 'sedan' (length=5)var_dump(json_decode('{a:"tan","model":"sedan"}'));//null

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.