Resolves an issue where the PHP server returns a JSON string with special characters

Source: Internet
Author: User
Tags php server ultraedit

1. Description of the problem

The JSON string returned by the backend interface found in the call to the PHP backend interface Gson has been parsed without:

List<Region> districts = null;        if (!TextUtils.isEmpty(myString))        {            Gson gson = new Gson();            try { districts = gson.fromJson(myString, new TypeToken<List<Region>>() {}.getType()); } catch (Exception e) { e.printStackTrace(); } }

The general description of the exception hint is that the first column of the first row expects the object array to begin, but actually the beginning of the string
The above mystring is a JSON string with the following contents:

2. Analysis Process

(1) Copy the above JSON string to the JSON online check, also did not pass
(2) The JSON string is saved in the file, the file is opened in the Chrome browser and the format of the JSON layout is not displayed;
(3) Carefully check the format, did not find any problem, according to the information of the exception prompt, it is likely that the first row of the first column has special characters exist
(4) Carefully view the above JSON string in the EditPlus editor and find [very special, not normal total brackets
(5) If manual modification into normal [, copy to JSON online check inside, JSON qualified, pass!
(6) Before the modified JSON string is saved as a file, the modified JSON string is saved as a file
(7) In order to see what the first character is, I use the UltraEdit editor to open the modified file and the modified file respectively.

Then the 16 binary view (edit->16 function, 16 binary edit)

JSON string with special string Errorcode_region.txt file 16 binary view content as follows:

No special characters, modified after normal JSON string new_regions.txt file 16 binary view content as follows:

By comparison found in the normal [symbol many special characters 16 binary is the EF BB BF

By looking at the relevant information it is the UTF-8 encoding of the BOM (byte order mark, byte order mark). That is, the JSON string is in UTF-8 format with the BOM.

The following is a description of the UTF-8 BOM, reference: UTF-8 BOM and no BOM differences

The UTF-8 does not require a BOM to indicate byte order, but it can be used to indicate the encoding using a BOM. The UTF-8 code for the character "ZERO WIDTH no-break SPACE" is the EF BB BF. So if the receiver receives a byte stream beginning with the EF BB BF, it knows that this is UTF-8 encoded.

In a UTF-8 encoded file, the BOM occupies three bytes. If you use Notepad to save a text file as UTF-8 encoding, open the file with your UE, switch to the hexadecimal edit State to see the beginning of the Fffe. This is a good way to identify the UTF-8 encoded file, the software through the BOM to identify whether this file is UTF-8 encoding, many software also requires that the file read must have a BOM. However, there are still many software that do not recognize the BOM.

In the early version of Firefox, there is no BOM in the extension, but the version after Firefox 1.5 has started to support the BOM. It is now also found thatPHP does not support the BOM. PHP did not consider BOM at design time, that is to say, he will not ignore the UTF-8 encoded file at the beginning of the BOM three characters.

3. Workaround

Now that the problem is known: PHP back in the UTF-8 format of the JSON string beginning with the BOM, the solution is to remove the BOM.

How to get rid of it? Can not be removed in the client, should be in the PHP background according to the interface name, find the interface corresponding to the code of the PHP file, the PHP file is saved in the format UTF-8 no BOM format.

(1) When saving with EditPlus, encode select UTF-8

If the UTF-8 +bom is selected, it contains the BOM

Note: When you open a file with a BOM with EditPlus, the uf-8+ is displayed at the bottom

When you open a file without a BOM, the UTF-8 is displayed at the bottom

(2) When saving with UltraEdit, encode select UTF-8 no BOM

If you choose UTF-8, it contains the BOM

Resolves an issue where the PHP server returns a JSON string with special characters

Related Article

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