我如何在JSONKit中JSON序列化NSDate字典 [英] How do I JSON serialize an NSDate Dictionary in JSONKit

查看:111
本文介绍了我如何在JSONKit中JSON序列化NSDate字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Jsonkit和Apple的JSON序列化器而没有运气。它一直打破geo属性,这是一个NSNumbers的数组。

I tried this using Jsonkit and Apple's JSON serializer with no luck. It keeps breaking on the geo property, which is an nsarray of NSNumbers.

Post* p = [[Post alloc] init];

    p.uname = @"mike";
    p.likes =[NSNumber numberWithInt:1];
    p.geo = [[NSArray alloc] initWithObjects:[NSNumber numberWithFloat:37.78583], [NSNumber numberWithFloat:-122.406417], nil ];
    p.place = @"New York City";
    p.caption = @"A test caption";
    p.date = [NSDate date];


 NSError* error = nil;

    NSString* stuff = [[p getDictionary] JSONStringWithOptions:JKParseOptionNone error:&error];

更新:检查错误是它失败的NSDate,而不是NSArray。如何将日期格式化程序传入函数?

UPDATE: Checking on the error it's the NSDate that it fails on, not the NSArray. How do I pass in the date formatter into the function?

更新2:解决了 - 查看了最新的jsonkit提交,看到你可以这样做:

UPDATE 2: Solved- ok looked at the latest commit for jsonkit and saw that you could do this:

 NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];

    NSString* result = [p.dictionary JSONStringWithOptions:JKSerializeOptionNone serializeUnsupportedClassesUsingBlock:^id(id object) {
        if([object isKindOfClass:[NSDate class]]) { return([outputFormatter stringFromDate:object]); }
        return(nil);
    } error:nil];

这似乎有效但注意JSONKit的这个功能是WIP所以它可能在下一个改变官方发布。

which seems to have worked but note that this feature for JSONKit is WIP so it could change in the next official release.

推荐答案

嗯 - 不能代表JSONKit或iOS5 - 我使用Stig的 SBJSON 框架。使用它实现相当简洁:

Hmmmm -- can't speak for JSONKit or iOS5 -- I use Stig's SBJSON framework. Using it the implementation is fairly succinct:

@implementation Post

- (id) initWithName:(NSString*)Name :(NSNumber*)Likes :(NSArray*)Geo :(NSString*)Place :(NSString*)Caption :(NSDate*)Date {

   if ((self=[super init])==nil) {
       return nil;
   }
   uname = Name;
   likes = Likes;
   geo = Geo;
   place = Place;
   caption = Caption;
   date = Date;
   return self;
}

- (NSDictionary*) getAsDictionary {
   NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
   [dateFormatter release];

   NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:uname,@"uname",
                      likes,@"likes",
                      geo,@"geo",
                      place,@"place",
                      caption,@"caption",
                      dateString,@"date",
                      nil];
   return dict;
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];

    Post* post = [[Post alloc] initWithName:@"Mike" 
                                       :[NSNumber numberWithInt:1] 
                                       :[[NSArray alloc] initWithObjects:[NSNumber numberWithFloat:37.78583], [NSNumber numberWithFloat:-122.406417],nil] 
                                       :@"New York City" :@"A Test caption" 
                                       :[NSDate date]];

    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
    NSString* json = [writer stringWithObject:[post getAsDictionary]];
    if (json == nil) {
        NSLog(@"error = %@",writer.errorTrace);
    }
    NSLog(@"json = %@",json);
    [writer release];
    [post release];
}

产生


TestJSON [52337:207] json = {likes:1,date:2011-12-13 11:12:57,place:纽约市,标题:测试标题,uname:Mike,geo:[37.78583,-122.4064]}

TestJSON[52337:207] json = {"likes":1,"date":"2011-12-13 11:12:57","place":"New York City","caption":"A Test caption","uname":"Mike","geo":[37.78583,-122.4064]}

这篇关于我如何在JSONKit中JSON序列化NSDate字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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