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 parsing failure mean?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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.

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

*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

Internet Technology

Wechat

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

12
Report