PHP Json_decode function Parsing JSON result NULL resolution method

Source: Internet
Author: User

When making the site CMS module, the Content field of the module contents is stored in JSON format, so the Json_decode () function is needed in the background for the editing of the module content (removing the saved data).

But at the time of parsing, the result of parsing with the Json_decode () function is always NULL and there is no array to parse. Here are the questions and analysis:

1. When outputting a JSON string, the code and page are displayed in the following:

Echo $content $res [0] [' Con '] [' content '];

Just consider $content, $res [0][' con '] [' content '] is the value of the content retrieved from the returned data, which is not to be considered here. This page displays:

{"Bpic": "Group1\/m00\/00\/0d\/rbak31sttzeae056aakwubmsagc339.jpg", "bname": "About1", "Breason": "3334" , "About2": "444", "ABOUT3": "555", "Tpic1": "Group1\/m00\/00\/0d\/rbak31stsygarpwhaamcd98u8xo736.jpg", "Tpic2": " Group1\/m00\/00\/0d\/rbak31sttzeaeneaaanwlboxmya393.jpg "," tpic3 ":" group1\/m00\/00\/0d\/ Rbak31sttsqarrumaakwubmsagc150.jpg "}

To beautify a little bit:

{
"Bpic": "Group1\/m00\/00\/0d\/rbak31sttzeae056aakwubmsagc339.jpg", "bname": "Breason", "ABOUT1": "3334", "About2": "444", "ABOUT3": "555", "Tpic1": "Group1\/m00\/00\/0d\/rbak31stsygarpwhaamcd98u8xo736.jpg", "Tpic2": " Group1\/m00\/00\/0d\/rbak31sttzeaeneaaanwlboxmya393.jpg "," tpic3 ":" group1\/m00\/00\/0d\/ Rbak31sttsqarrumaakwubmsagc150.jpg "}

2. Use Json_decode () to resolve the $content and print using Var_dump:

$content = Json_decode ($content,true);

However, the page shows NULL. At this point, use the Json_last_error () function to print the error, the page displays 4, which is a syntax error.

Echo $errorinfo = Json_last_error ();   //Output 4 syntax error

Workaround:

This problem occurs because the double quotes are escaped in the JSON string, and only the Htmlspecialchars_decode () function is used to handle the $content:

$content = Htmlspecialchars_decode ($content);

At this point, using the Json_decode () function parsing, there is no problem, page output:

Array
[bname] =3334= 555
[Tpic2] = group1/m00/00/0d/rbak31sttzeaeneaaanwlboxmya393.
)

PHP Json_decode function Parsing JSON result NULL resolution method

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.