Json string parsing failure

Source: Internet
Author: User
The problem that json strings cannot be parsed is that I made a PHP interface and the returned data is in json format (the data format is correct and the encoding is in utf8 format ); however, this json format data cannot be parsed during the call, but I can parse the returned data by copying it into a string. After searching for half a day, I found that there was a blank space in front of the output source code. However, this blank space cannot be solved through trim's space removal method, simulating the request interface, trim ($ result, "\ xEF \ xBB \ xBF") using the bom header; still cannot be resolved. Have you ever encountered this problem?


Reply to discussion (solution)

That is, if the json_encode output block is not written, whether the header is declared ('content-type: application/json; charset = utf-8 ');

It should be the impact of the BOM header.
$ Result = trim ($ result, "\ xEF \ xBB \ xBF ");
Only remove the BOM in $ result
The BOM of your php program file is not removed.

Create a new program

echo bin2hex(file_get_contents('http://yourhost/api.php'));
After j is executed, you can see whether BOM exists.

It should be the impact of the BOM header.
$ Result = trim ($ result, "\ xEF \ xBB \ xBF ");
Only remove the BOM in $ result
The BOM of your php program file is not removed.

Create a new program

echo bin2hex(file_get_contents('http://yourhost/api.php'));
After j is executed, you can see whether BOM exists.



It should be the impact of the BOM header.
$ Result = trim ($ result, "\ xEF \ xBB \ xBF ");
Only remove the BOM in $ result
The BOM of your php program file is not removed.

Create a new program

echo bin2hex(file_get_contents('http://yourhost/api.php'));
After j is executed, you can see whether BOM exists.



It should be the impact of the BOM header.
$ Result = trim ($ result, "\ xEF \ xBB \ xBF ");
Only remove the BOM in $ result
The BOM of your php program file is not removed.

Create a new program

echo bin2hex(file_get_contents('http://yourhost/api.php'));
After j is executed, you can see whether BOM exists.



We can see that these outputs are available at the frontend.
Bytes

How can I remove the bom header from the file?

First 2020 efbbbf
2020 indicates there are two spaces
Isn't efbbbf bom?

It should be the impact of the BOM header.
$ Result = trim ($ result, "\ xEF \ xBB \ xBF ");
Only remove the BOM in $ result
The BOM of your php program file is not removed.

Create a new program

echo bin2hex(file_get_contents('http://yourhost/api.php'));
After j is executed, you can see whether BOM exists.

Thanks for the enthusiastic answers from the moderator. The problem is finally solved. after checking all the relevant files, I found the file containing the bom header and edited it again to parse the json.

Notepad ++ converts a file to a non-BOM format.

You can see this log: http://my.oschina.net/rain21/blog/384549

Notepad ++ converts a file to a non-BOM format.

You can see this log: http://my.oschina.net/rain21/blog/384549

Thank you for your answers.

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.