我使用以下代码解析了Json
- (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];
}
输出如下
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
如何从NSDictionary获取NSString中的时间段?
先谢谢。
答案 0 :(得分:2)
您可能需要稍微调整一下,但这是我在这些日期中使用的内容:
标题文件
@interface NSDate (JSON)
+ (NSDate *)dateFromJSON:(NSString *)dateString;
@end
<强>实施强>
#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
此示例不包含您日期中的时区,因此您可能希望添加该时区。
然后使用它:
for (NSString *dateValue in parsedData) {
NSDate *date = [NSDate dateFromJSON:dateValue];
}