ios - 如何在iOS中解析nsmutabledictionary时间?

标签 ios objective-c json

我使用以下代码解析了 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 中的时间段?

提前致谢。

最佳答案

您可能需要对此进行一些调整,但这就是我对此类日期使用的方法:

头文件

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

关于ios - 如何在iOS中解析nsmutabledictionary时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23382544/

相关文章:

ios - 如何在项目之间添加ARC

c# - 尝试使用 PCLStorage 读取文件时卡住

ios - Fabric Archive 打包错误-6

objective-c - 使用对象填充 NSMutableSet 不起作用 - NSLog 和调试器显示 Null

ios - NSHTTPCookie 保存错误

c# - Json XmlDictionaryReader 无法读取包含空格的值

javascript - 将 JSON 解析为 HTML 表时未定义

ios - iOS 上的 Cocos2d-x Admob

objective-c - 在类扩展中声明新属性在 Objective-C 中是一种不好的做法吗?

JSON Postman 正文不适用于 PostgreSQL 插入