2

I have parsed the Json using following code

- (void) getWorkingCalendar:(NSData *)dataForParse{

    NSMutableArray *locations = [[NSMutableArray alloc] init];

    NSString* myString;
    myString = [[NSString alloc] initWithData:dataForParse encoding:NSASCIIStringEncoding];

    NSLog(@"data is %@", myString);

    NSArray * parsedData = [NSJSONSerialization JSONObjectWithData:dataForParse options:kNilOptions error:nil];

    NSLog(@"parser data %@", parsedData);


    for (NSDictionary *dict in parsedData) {


        NSLog(@"Value is %@", dict);

    }

    [delegate array:locations];


}

Output as below

2014-04-30 12:44:41.828 Testing[827:60b] parser data (
    "/Date(1399016682788+0400)/",
    "/Date(1399621482788+0400)/",
    "/Date(1400226282788+0400)/",
    "/Date(1400831082788+0400)/",
    "/Date(1401435882788+0400)/"
)
2014-04-30 12:44:41.829 Testing[827:60b] Value is /Date(1399016682788+0400)/
2014-04-30 12:44:41.829 Testing[827:60b] Value is /Date(1399621482788+0400)/
2014-04-30 12:44:41.829 Testing[827:60b] Value is /Date(1400226282788+0400)/
2014-04-30 12:44:41.830 Testing[827:60b] Value is /Date(1400831082788+0400)/
2014-04-30 12:44:41.830 Testing[827:60b] Value is /Date(1401435882788+0400)/
2014-04-30 12:44:41.830 Testing[827:60b] finishDownloading

How can I get time slot in NSString from NSDictionary?

Thanks in Advance.

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

1 Answers1

2

You might need to tweak this a bit, but this is what I've used with these kind of dates:

Header File

@interface NSDate (JSON)

+ (NSDate *)dateFromJSON:(NSString *)dateString;

@end

Implementation

#import "NSDate+JSON.h"

@implementation NSDate (JSON)

+ (NSDate *)dateFromJSON:(NSString *)dateString {

    if (dateString == nil || [dateString isKindOfClass:[NSNull class]]) {
        return nil;
    }

    NSString *header = @"/Date(";
    NSRange headerRange = [dateString rangeOfString:header];
    if (headerRange.location != 0) {
        return nil;
    }

    NSString *footer = @")/";
    NSRange footerRange = [dateString rangeOfString:footer options:NSBackwardsSearch];
    if (footerRange.location == NSNotFound || footerRange.location + footerRange.length != dateString.length) {
        return nil;
    }

    NSString *timestampString = [dateString substringWithRange:NSMakeRange(headerRange.length, footerRange.location - headerRange.length)];

    NSTimeInterval interval = [timestampString doubleValue] * 0.001;
    return [NSDate dateWithTimeIntervalSince1970:interval];
}

@end

This example does not include the time zone that is in your date, so you might want to add that.

Then use it like:

for (NSString *dateValue in parsedData) {
     NSDate *date = [NSDate dateFromJSON:dateValue];
}
rckoenes
  • 69,092
  • 8
  • 134
  • 166