Null issues with JSON parsing in iOS

Source: Internet
Author: User

Data communication with the server is often required during iOS development, and JSON is a common, efficient and concise data format.

problem Phenomenon

But a few projects have been encountered a problem of the pit dad, the program after some data to get some inexplicable collapse. It was a long time ago to find out why: Because some fields in the server's database are empty and then returned to the client in JSON form, this data appears:

"somevalue": null

The data parsed by this third-party library is Jsonkit.

Somevalue = "<null>";

This data type is either nil or String. Once parsed into an object, sending a message directly to the object (Eg:length,count, and so on) will crash directly. The prompt error is:

-[nsnull length]: Unrecognized selector sent to instance 0x388a4a70
Workaround

In fact, has not found the perfect solution, pit me for a long time.

1, the first solution is to cope with the current crash, to see which field may be empty, then the field before the use of judgment, through the crash of error hints can be seen, such a field resolved to the object is NSNull type, so you can directly determine whether this type:

if (![ Iskindofclass:[nsnull class]]) {xxxxxxx;}

Because there are too many fields, look for a complement.

2, later want to completely solve this problem, it is intended to start from the data source, in fact, should be able to use regular expression matching this null, and then replace, but I was the mishap. So it came out a cottage method: string matching. When retrieving the JSON returned by the server, the string object is returned, substituting null for the null character "" before parsing.

JSON = [jsonstr stringbyreplacingoccurrencesofstring:@ ": null" withstring:@ ": \" \ ""];

This method would have worked well, but my server here returned very not concise, all kinds of garbage data (do not spit groove this) ... Anyway, this will cause JSON to fail to parse.

3, in the end there is no way, only in the resolution of the time, the value of the Nsnull type is replaced with nil. Usually write a tool method, and then parse the call. But too much trouble, just want to write a macro, through the search surprised to find the macro can also have a return value, the result is as follows:

#define Verifyvalue (value) ({ID tmp;if ([Value Iskindofclass:[nsnull class]) tmp = NIL;ELSETMP = value;tmp;}) \

The last sentence in the macro is the return value. The macro is then called when parsing the data:

Contact.contactphone = Verifyvalue (contactdic[@ "Send_contactphone"]);

4, if you use afnetwork this library to do network requests, you can use the following code, automatically help you remove this annoying empty value

Self.removeskeyswithnullvalues = YES;

5. The Ultimate Solution
Finally found once and for all, the foreigner wrote a category, called Nullsafe, operating at run time, this annoying empty value is set to nil, and nil is safe, you can send any message to the nil object without rushing to collapse. This category is very convenient to use, as long as the project can be added, you do not have to do anything else, yes, it is so simple. For details please go to GitHub to view;
Https://github.com/nicklockwood/NullSafe


Null issues with JSON parsing in iOS

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.