The BOM header for the deep pit of JSON parsing

Source: Internet
Author: User

Preface: When we deal with JSON strings, we often encounter this problem org.json.JSONException:Value of type java.lang.String cannot be converted to Jsonobject, The Jsonobject object throws this exception when parsing the JSON string returned by the server. In fact, this is the return JSON string containing the BOM header caused.


Ben is not good at writing. Hope you prawns forgive me!!!


what is JSON?  

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).

what is a BOM header?

in the Utf-8 encoded file, the BOM in the file header, occupies three bytes, used to indicate that the file belongs to the Utf-8 encoding, now has a lot of software to identify the BOM header, but also some do not recognize the BOM header, such as PHP does not recognize the BOM header, This is also the reason why the error occurs when you edit Utf-8 encoding with Notepad.

to remove the BOM header:

1, EditPlus to the BOM head method
After the editor is resized to UTF8 encoded format, the saved file will be preceded by a string of hidden characters (also known as BOMs), used by the editor to identify whether the file is encoded in UTF8.
Run EditPlus, click Tools, select Preferences, select File, UTF-8 identity Select Always delete signature,
then the PHP files are edited and saved by the PHP file is not with the BOM.

2, UltraEdit removal of BOM head method
after opening the file, Save As option in the encoding format selected (Utf-8 without BOM header), OK is OK


The use of JSON is almost a year, and have encountered a number of inexplicable parsing error! The reason is because the JSON file generated in Utf-8 has a BOM header

For example, the JSON in this picture below

650) this.width=650; "src=" Http://www.echohoo.com/content/uploadfile/201607/2b341467527376.png "title=" Click to view original "alt = "Bom_json.png" border= "0" height= "465" width= ""/> "

I rookie a piece, hope you shrimp light sprayed ~ Thank you!!!

Original link: http://www.echohoo.com/?post=515

This article is from the "11105560" blog, please be sure to keep this source http://11115560.blog.51cto.com/11105560/1795299

The BOM header for the deep pit of JSON parsing

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.