1
NSString *date = [userDefaults objectForKey:KEY_FORM7_DECLARATION_DATE];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:date, @"DATE",nil]
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *result = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

When I do NSLog(@"%@", date) It shows me 2016/10/30 But When I try to print NSLog(@"%@", result) the date changes to 2016\/10\/30. Any help would be appreciated.

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
sagar
  • 379
  • 4
  • 13
  • @Paulw11 I even tried to write the json to text file but it shows the same. – sagar Sep 30 '16 at 07:06
  • Can you tell me what is the value for [userDefaults objectForKey:KEY_FORM7_DECLARATION_DATE] – Himanth Sep 30 '16 at 08:04
  • It is String format of date i.e., "2016/10/10" @himanth – sagar Sep 30 '16 at 08:39
  • try like this NSString *date = [userDefaults objectForKey:KEY_FORM7_DECLARATION_DATE]; NSString *main = [NSString stringWithFormat:@"{\"DATE\":\"%@\"}",date]; NSData *mainData = [main dataUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%@",[[NSString alloc]initWithData:mainData encoding:NSUTF8StringEncoding]); – Himanth Sep 30 '16 at 08:47
  • Did you tried that? – Himanth Sep 30 '16 at 08:54
  • @himanth It is of no use because i need to add it to the dictionary with my other parameters so that I will be able to do NSJSONSerialization but hey thanks anyways :) – sagar Sep 30 '16 at 09:00

1 Answers1

0

Seems like it's intended behaviour of NSJSONSerialization: similar question

For your simple case I can only come up with simple workaround: serialize the JSON with substitution sign and then use serialized string as a format:

let dateString = "2016/10/30"
var dict = ["DATE": "%@"]

do {
    let data = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions(rawValue: 0))
    let format = String(data: data, encoding: NSUTF8StringEncoding)!
    let result = String(format: format, dateString)
} catch {
    print(error)
}
Community
  • 1
  • 1
slashdot
  • 630
  • 5
  • 14