Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON dates on IPhone

Forgive me as I'm new to Objective C.

I am getting back dates from a .NET webservice in the /Date(xxxxxxxxxxxxx-xxxx)/ format. I'm looking for some direction on how to best parse this into an NSDate object. I've tried using dateWithTimeIntervalSince1970 on it but it comes back with a date in the year 1969 for a date I know is in 2006.

Looking for some direction on the proper way to handle JSON dates.

Thanks in advance!

like image 358
user213517 Avatar asked Nov 18 '09 16:11

user213517


People also ask

How do I open JSON files on my iPhone?

With JSON VIEWER, opening, searching and managing JSON files on your iPhone or iPad will no longer be a problem. To use it, touch the JSON file in any application, select "Open with .." And select JSON VIEWER.

What is JSON parsing in iOS?

JSON parsing in Swift is a common thing to do. Almost every app decodes JSON to show data in a visualized way. Parsing JSON is definitely one of the basics you should learn as an iOS developer. Decoding JSON in Swift is quite easy and does not require any external dependencies.

Does iPhone use JSON?

JSON is a popular data-interchange format used by APIs (application programming interfaces). JSON lets you bundle a large amount of data into one chunk of text and then send it along to another service.

How do I parse JSON?

Use the JavaScript function JSON. parse() to convert text into a JavaScript object: const obj = JSON. parse('{"name":"John", "age":30, "city":"New York"}');


2 Answers

I just wrote this for iOS 4.0+ (because it uses NSRegularExpression). It handles dates with or without timezone offsets. Seems to work pretty well, what do you think?

+ (NSDate *)mfDateFromDotNetJSONString:(NSString *)string {     static NSRegularExpression *dateRegEx = nil;     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];     });     NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];      if (regexResult) {         // milliseconds         NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;         // timezone offset         if ([regexResult rangeAtIndex:2].location != NSNotFound) {             NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]];             // hours             seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;             // minutes             seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;         }          return [NSDate dateWithTimeIntervalSince1970:seconds];     }     return nil; } 
like image 192
jasongregori Avatar answered Sep 19 '22 20:09

jasongregori


I was in the same boat whilst using json-framework which doesn't support the date format as it's not official JSON. My source is from an API built using JSON.Net. This is what I came up with:

- (NSDate*) getDateFromJSON:(NSString *)dateString {     // Expect date in this format "/Date(1268123281843)/"     int startPos = [dateString rangeOfString:@"("].location+1;     int endPos = [dateString rangeOfString:@")"].location;     NSRange range = NSMakeRange(startPos,endPos-startPos);     unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];     NSLog(@"%llu",milliseconds);     NSTimeInterval interval = milliseconds/1000;     return [NSDate dateWithTimeIntervalSince1970:interval]; } 

I don't have the appended portion in the date format that you do so I haven't dealt with that like the answer above. No error catching either, it's all new to me at this point.

like image 22
toxaq Avatar answered Sep 18 '22 20:09

toxaq