0

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 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"?

RickiG
  • 11,380
  • 13
  • 81
  • 120

1 Answers1

2

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.

  • Hi Graham, thanks for the suggestion. I wasn't up for rewriting the scanner part of the TouchJSON so instead I loop the data returned and replace the empty strings with @"" and the empty numbers with 0. I get to do this because I only deal with Strings and Numbers and my data goes directly into views that are fine with @"". – RickiG Nov 10 '10 at 15:56
  • 1
    Graham, Which file did u change? – RK- Mar 31 '11 at 14:16