Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does json parsing failure mean?

Shulou Source: shulou.com Published: 2022-05-31 10:38:41 09月21日 Update

This article is a small series for everyone to introduce in detail "what json analysis failure refers to", the content is detailed, the steps are clear, the details are properly handled, I hope this article "what json analysis failure refers to" can help everyone solve doubts, the following follow the small series of ideas slowly in-depth, together to learn new knowledge.

1. format of

(1) Format error

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.

(2) The format is correct, but the report is wrong.

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.

2. Ensure that key values in JSON data are of type String

If the key value of JSON data is non-String type, use NSJSONSerialization.

Parsing will not find the key and report an error

3. JSON parsing boolean type data field results in error

BOOL manage = [content objectForKey:@"manage"];

The result above is that regardless of whether the manage field in the JSON data is 0 or 1, manage is YES.

When receiving JSON fields with BOOL, you need to add boolValue conversion:

BOOL management = [[contentobjectForKey:@"management"] boolValue]; Read here, this article "json parsing failure refers to what" article has been introduced, want to master the knowledge of this article also need to practice to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.

Tags: Encoding character error data type utf-8 field quotation mark article format three content parameter string time empty character result Chinese foreign language proper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Xiaomi Huawei OPPO Reno