在 iPhone 上解析 JSON 日期 [英] Parsing JSON dates on IPhone

查看:25
本文介绍了在 iPhone 上解析 JSON 日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅我,因为我是 Objective C 的新手.

Forgive me as I'm new to Objective C.

我从/Date(xxxxxxxxxxxxx-xxxx)/格式的 .NET 网络服务中获取日期.我正在寻找有关如何最好地将其解析为 NSDate 对象的方向.我试过在它上面使用 dateWithTimeIntervalSince1970,但它返回的日期是 1969 年,因为我知道它是 2006 年的日期.

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.

正在寻找有关处理 JSON 日期的正确方法的方向.

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

提前致谢!

推荐答案

作为一名学习 Objective-C 的 .NET 程序员,我在尝试使用 .Net WebService 时遇到了同样的问题.

As a .NET programmer learning Objective-C I had the same problem when I tried to consume a .Net WebService.

起初我以为我可以使用 NSDateFormatter...我发现它的符号非常好参考 here,但我很快意识到我需要将数字从毫秒转换为秒.

At first I thought I would be able to use the NSDateFormatter... I found a really good reference for it's symbols here, but I quickly realized that I needed to convert the number from milliseconds to seconds.

我写了代码来做到这一点......我还在学习 Obj-C,但我认为它不应该这么难......

I wrote the code to do it... I'm still learning Obj-C but I dont think It should've been this hard...

- (NSDate *) getJSONDate{
    NSString* header = @"/Date(";
    uint headerLength = [header length];

    NSString*  timestampString;

    NSScanner* scanner = [[NSScanner alloc] initWithString:self];
    [scanner setScanLocation:headerLength];
    [scanner scanUpToString:@")" intoString:&timestampString];

    NSCharacterSet* timezoneDelimiter = [NSCharacterSet characterSetWithCharactersInString:@"+-"];
    NSRange rangeOfTimezoneSymbol = [timestampString rangeOfCharacterFromSet:timezoneDelimiter];

    [scanner dealloc];

    if (rangeOfTimezoneSymbol.length!=0) {
        scanner = [[NSScanner alloc] initWithString:timestampString];

        NSRange rangeOfFirstNumber;
        rangeOfFirstNumber.location = 0;
        rangeOfFirstNumber.length = rangeOfTimezoneSymbol.location;

        NSRange rangeOfSecondNumber;
        rangeOfSecondNumber.location = rangeOfTimezoneSymbol.location + 1;
        rangeOfSecondNumber.length = [timestampString length] - rangeOfSecondNumber.location;

        NSString* firstNumberString = [timestampString substringWithRange:rangeOfFirstNumber];
        NSString* secondNumberString = [timestampString substringWithRange:rangeOfSecondNumber];

        unsigned long long firstNumber = [firstNumberString longLongValue];
        uint secondNumber = [secondNumberString intValue];

         NSTimeInterval interval = firstNumber/1000;

        return [NSDate dateWithTimeIntervalSince1970:interval];
    }

    unsigned long long firstNumber = [timestampString longLongValue];
    NSTimeInterval interval = firstNumber/1000;

    return [NSDate dateWithTimeIntervalSince1970:interval];
}

希望有人可以提供更好的 Obj-C 解决方案.如果没有,我可能会保留这个或寻找一种方法来更改 .NET 中的序列化格式

Hopefully someone can provide a better Obj-C solution. If not I may keep this or look for a way to change the serialization format in .NET

关于 JSON 日期时间格式...如果您对服务有任何控制权,最好将日期转换为 DataContract 对象中的字符串.

About that JSON DateTime format... If you have any control on the service it would probably be best to convert the date to a string in your DataContract objects.

格式化为 RFC1123 现在对我来说似乎是个好主意.因为我可以使用 NSDateFormatter 轻松获取它.

Formatting to RFC1123 seems like a good idea to me right now. As I can probably pick it up easily using a NSDateFormatter.

引自 Rick Strahl

没有 JavaScript 日期文字,Microsoft 设计了一种自定义日期格式,它本质上是一个标记字符串.格式是一个经过编码的字符串,其中包含标准的新日期(自 1970 年以来的毫秒数)值.

There's no JavaScript date literal and Microsoft engineered a custom date format that is essentially a marked up string. The format is a string that's encoded and contains the standard new Date(milliseconds since 1970) value.

这篇关于在 iPhone 上解析 JSON 日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆