Detect null and "null"-like JSON values? xcode

Tidane picture Tidane · May 31, 2012 · Viewed 7.8k times · Source

I have a problem with inserting data to a cell in tableView. When I searched for the source of error I saw that my webservice is returning null values. So when I call the name to the cell it crashes argumenting that is null. So, cause is a database issue, I just want to skip adding the ones that have for example: { "name":"null"}

In others services, i get null values empty... So It does not cause problems.. I think it is the null value as a string.

Here is an example:

{
    "24_7" = 0;
    address = "avenue innsbruck";
    city = GRENOBLE;
    country = FR;
    dst = "14007.2046741012";
    "h_saturday" = " - ";
    "h_sunday" = " - ";
    "h_week" = " - ";
    id = 324;
    "location_lat" = "45.154";
    "location_long" = "5.73387";
    name = "<null>";
    "tel_1" = 0476544254;
    "tel_2" = "";
    zipcode = 38000;
}

I just want to skip adding this when I'm parsing the json in here.

NSString *theJSON = [request responseString];

SBJsonParser *parser = [[SBJsonParser alloc] init];

NSDictionary *jsonDictionary = [parser objectWithString:theJSON error:nil];

//NSLog(@"OkRequest || %@ ", jsonDictionary);

[_jsonStation removeAllObjects];
for (NSDictionary *taxi in jsonDictionary)
{

    [_jsonStation addObject:taxi];

}

Answer

Cyrille picture Cyrille · May 31, 2012

You can test if your object is null by doing this in your loop:

if ([taxi isKindOfClass:[NSNull class]])
    continue;
//else, process it