开发者

Dealing with <'null>' values in TouchJSON

开发者 https://www.devze.com 2023-01-24 17:12 出处:网络
I am deserializing some JSON using the TouchJSON framework. The array of dictionaries that comes from the parsing is used to populate a UITableView.

I am deserializing some JSON using the TouchJSON framework. The array of dictionaries that comes from the parsing is used to populate a UITableView.

There is a chance that some of the values in the JSON I parse are empty. This results, if I NSLog it to the console, in the values looking like this:

id = 1234;
title = "Hello, World";
description = "<null>"; 
detail = "The world says hello";

Here the description value was an empty string when retrieved from the server.

So TouchJSON recognizes that the description values is of type string, but the original intention of the server was to communicate that this was an empty string, like description = @"";

If I later on try to set the value of description, to a UILabels text property the app will crash.

So my questions are, I have both NSNumbers and NSStrings in the JSON, should I traverse the result from TouchJSON's deserialize method and test all values and how would I开发者_StackOverflow do so?

I can't simulated what would happen if an NSNumber value was empty, how would I test for this? Will the NSNumber value be nil in that case instead of "null"?


I was using the SBJSON library and came up against the same problem. My solution would apply to your case too: I changed the library so that it handled missing values, setting them to +[NSNull null] in the collection it returned. That makes your client code a little warty, because you have to handle the cases where you might get an NSNull instead of an NSString. But this is just a more obvious version of the wart where you have to decide whether @"" meant an empty string or an unset value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号