Json parsing in iOS contains null, nil, NSNumber, and jsonnsnumber.

Source: Internet
Author: User

Json parsing in iOS contains null, nil, NSNumber, and jsonnsnumber.

During iOS development, data is often required to communicate with the server. Json is a common and efficient and concise data format.

Symptom

However, after several projects, the program crashes unexpectedly after obtaining some data. As a matter of fact, the cause has been discovered for a long time: because some fields in the database of the server are empty, such data will appear when they are returned to the client in Json format:

?
1 "somevalue":null

The data parsed from the third-party library JsonKit becomes

?
1 somevalue = "<null>";

This data type is neither nil nor String. After it is parsed into an object, if the message (such as: length, count, etc.) is sent directly to this object, it will crash. The error message is:

?
1  -[NSNull length]: unrecognized selector sent to instance 0x388a4a70
Solution

In fact, I have never found a perfect solution.

1. The initial solution is to cope with the current crash and check which field may be empty. Then, make a judgment before using this field. The error prompt during the crash shows that, this field is parsed into an object of the NSNull type, so you can directly determine whether it is of this type:

?
1  if (![isKindOfClass:[NSNull class]]){xxxxxxx;}

Because there are too many fields, you can add one.

2. If I want to completely solve this problem later, I plan to start from the data source. In fact, I should be able to use a regular expression to match this null and then replace it. But I am sorry that regular expressions are hard to solve. As a result, a shanzhai method is developed: string matching. When obtaining the Json returned by the server and returning the result as a string object, replace null with a null Character "" before parsing.

?
1 json = [jsonStr  stringByReplacingOccurrencesOfString:@":null" withString:@":\"\""];

This method has worked very well, but the server returns a lot less concise and spam data (No spam )... In this case, json cannot be parsed.

3. There is no final solution. You can only start the parsing and replace the value of the NSNull type with nil. Generally, write a tool method and call it during parsing. If it is too troublesome, you just want to write a macro. by searching for the macro, you can find that the macro can return values. The result is as follows:

?
12345678 #define VerifyValue(value)\({id tmp;\if ([value isKindOfClass:[NSNull class]])\tmp = nil;\else\tmp = value;\tmp;\})\

The last statement in the macro is the return value. Then call the Macro when parsing the data:

?
1 contact.contactPhone = VerifyValue(contactDic[@"send_ContactPhone"]);

4. If you use the AFNetwork library for network requests, you can use the following code to automatically remove this annoying null value.

?
1 self.removesKeysWithNullValues = YES;

5. Ultimate Solution
Finally, I found a permanent solution. A great foreigner wrote a Category called NullSafe. during operation, I set this annoying null value to nil, while nil is safe, you can send any message to the nil object without crashing. This category is very convenient to use. You only need to add it to the project. You don't need to do anything else. For details, go to Github;
Https://github.com/nicklockwood/NullSafe

 

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