NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'

前端 未结 8 2395
南旧
南旧 2021-02-04 01:05

I\'m getting this exception when I try to JSON encode NSDate object.I believe NSDate is not compatible for JSON encoding. but I must encode the date.Any solutions?



        
8条回答
  •  感动是毒
    2021-02-04 01:16

    Have you tried ?

    updateDate = [NSNumber numberWithFloat:[[NSDate date] timeIntervalSince1970]];
    

    As Described Here : SDK not supporting NSDate objects

    Follow these Steps :

    1. Convert the Date in JSON Format :

     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
     [formatter setDateFormat:@"Z"];
     NSString *updateDate = [NSString stringWithFormat:@"/Date(%.0f000%@)/", [[NSDate date] timeIntervalSince1970],[formatter stringFromDate:[NSDate date]]];
    

    2. Embed In some array and POST the array.

提交回复
热议问题