3

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];

}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
Tidane
  • 694
  • 1
  • 10
  • 22

1 Answers1

12

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

if ([taxi isKindOfClass:[NSNull class]])
    continue;
//else, process it
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Thank you sir... At the end I did this: if ([[taxi objectForKey:@"name"] isKindOfClass:[NSNull class]]) { continue; } else { [_jsonStation addObject:taxi]; } – Tidane May 31 '12 at 15:41
  • 1
    You don't even need the "else", as the "continue" kicks you out of the "if...else" block. – Cyrille May 31 '12 at 18:32
  • I have a quick question here, I was working in the same kind of the app. I had some json data, lets say zip code for example. Its an integer, right? I get the zip data from json file from remote web service and when I tried to compare like: ( if int myZip == 10987 ) this is not working. In fact the json returned value of zip is not an integer as it is supposed to be, it says its an id, not an integer. How can I compare like I showed above? Please help. – jeewan Dec 29 '12 at 21:27
  • Integers, as other numbers, are stored as `NSNumber`, which is an object wrapper for scalar values. You can retrieve them using `int zipAsInteger = [myZip intValue]`. Please also keep in mind than in France, zip codes can start with a zero, so you'd better store them as strings instead. – Cyrille Dec 30 '12 at 09:51
  • Also note than with the newest SDKs, you can use `taxi[@"name"]` instead of `[taxi objectForKey:@"name"]`, which is much shorter and readable. That's called ''indexed subscripting''. – Cyrille Dec 30 '12 at 09:53
  • @Cyrille Thank you so much! My app used to crash because certain parts of my JSON file had null data. Now I can detect them which is of course very important. Thanks :) – Supertecnoboff Jan 25 '14 at 15:06