Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does json format error mean?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Json format error is what it means, I believe many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

malformed

Since json only accepts utf-8 encoded characters, the argument to json_encode() must be utf-8 encoded, otherwise it will get null characters or null. Special attention should be paid to this point when GB2312 coding is used for Chinese or ISO-8859-1 coding is used for foreign languages.

$bad_json = "{ 'bar': 'baz' }";$bad_json = '{ bar: "baz" }';$bad_json = '{ "bar": "baz", }';

Executing json_decode() on all three strings returns null and an error.

The first error is that json delimiters are only allowed in double quotes, not single quotes. The second error is that the "name" of the json name-value pair (the part to the left of the colon) must always be in double quotes. The third error is that you cannot add a trailing comma after the last value.

Also, json can only be used to represent objects and arrays, and json_decode() returns null if used on a string or value.

The format is correct, but the error is reported

The first type, character encoding problems

Since json only accepts utf-8 encoded characters, the argument to json_encode() must be utf-8 encoded, otherwise it will get null characters or null. Special attention should be paid to this point when GB2312 coding is used for Chinese or ISO-8859-1 coding is used for foreign languages.

The second type of BOM problem

BOM header is also called UTF-8 signature. In fact, BOM of UTF-8 has no effect on UFT-8. It is a BOM added to support UTF-16 and UTF-32. The meaning of BOM signature is to tell the editor what encoding the current file uses, so that the editor can recognize it. However, although BOM is not displayed in the editor, it will produce output, just like an extra blank line. If the general php code is edited and saved by Notepad and other software, when saving a file encoded with UTF-8, Three invisible characters (0xEF 0xBB 0xBF, BOM) are inserted at the beginning of the file. It is a string of hidden characters used by editors such as Notepad to identify whether the file is encoded in UTF-8. (It's unclear why the BOM header is automatically added to the returned data.)

/** * Remove bom header */public static String formatString(String s) {if (s != null) { s = s.replaceAll("\uffff", "");} return s;} After reading the above, do you know what json format error means? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report