我如何在JSONKit中JSON序列化NSDate字典

时间:2011-12-13 18:19:00

标签: objective-c ios json jsonkit

我尝试使用Jsonkit和Apple的JSON序列化器没有运气。它不断打破geo属性,这是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。如何将日期格式化程序传入函数?

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

 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,因此它可能会在下一个正式版本中发生变化。

2 个答案:

答案 0 :(得分:7)

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

@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”,“地点”:“纽约市”,“标题”:“测试标题“,”uname“:”Mike“,”geo“:[37.78583,-122.4064]}

答案 1 :(得分:0)

您必须将秒数乘以1000才能获得要发送到服务器端的正确长号(或其他)。

[NSNumber numberWithLongLong:[yourDate timeIntervalSince1970]*1000]

示例:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:uname,@"uname",
                      likes,@"likes",
                      geo,@"geo",
                      place,@"place",
                      caption,@"caption",
                      [NSNumber numberWithLongLong:[yourDate timeIntervalSince1970]*1000],@"date",
                      nil];