Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9150777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:39:23+00:00 2026-06-17T11:39:23+00:00

I try to add NSDate to NSMutableDictionary , and then make it a JSon

  • 0

I try to add NSDate to NSMutableDictionary, and then make it a JSon string with this methods:

NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
[d setObject:[NSDate date] forKey:@"date"];
NSData *jsondata = [[CJSONSerializer serializer] serializeDictionary:d error:nil];
NSString *requestBody = [[[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding] autorelease];

And i have couple of issue:
1) The jsondata is nil, and then the requestBody is nil too.
2) The [NSDate date] getting me wrong date, not like the date in my iPhone:

2013-01-19 11:48:46 +0000

Any help with this issue?

Edit:

When i print the error i got this msg:

Error Domain=TODO_DOMAIN Code=-1 "Cannot serialize data of type '__NSDate'" UserInfo=0x177850 {NSLocalizedDescription=Cannot serialize data of type '__NSDate'}

It’s not possible to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:39:24+00:00Added an answer on June 17, 2026 at 11:39 am

    It seems very unlikely that the dictionary would fail to insert the date. It seems much more likely that the problem is with the CJSONSerializer.

    Try the following:

    NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
    [d setObject:[NSDate date] forKey:@"date"];
    
    NSError *error = nil;
    NSData *jsondata = [[CJSONSerializer serializer] serializeDictionary:d error:&error];
    
    if (!jsonData)
    {
        NSLog(@"Error %@ while serialising to JSON");
    }
    
    NSString *requestBody = [[[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding] autorelease];
    

    You should see an error printed to the console.

    UPDATE

    OK, so it looks like we were right, and CJSONSerializer can’t serialise dates. So you need to turn your date into a string. This is done with NSDateFormatter.

    NSMutableDictionary *d = [[NSMutableDictionary alloc]init];
    NSDate *date = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    [d setObject:dateString forKey:@"date"];
    
    NSError *error = nil;
    NSData *jsondata = [[CJSONSerializer serializer] serializeDictionary:d error:&error];
    
    if (!jsonData)
    {
        NSLog(@"Error %@ while serialising to JSON");
    }
    
    NSString *requestBody = [[[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding] autorelease];
    

    Hopefully you should now get the formatted date in your JSON. You can vary the format by changing the string you pass to stringWithDate:. If you’re doing this a lot, you’ll want a single date formatter as creating them is costly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this error when try to add an item to my repositories/context: Collection has
I try to add admob, but I have some problem with it. This is
I have this error when I try add ace in class: Warning: PDO::quote() expects
This is the VBA code im using to try add a new sheet to
I try to display a date with a dateformatter but this one change the
I try add example from http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html to my site, this is code: news_model <?php
I have the below method and I want to try add in String.Compare public
i try to add facebook like button on my root domain that will like
I try to add the android coverflow in my own android projet when myt
I try to add dots between the page title and the page number in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.