How do I prevent NSJSONSerialization from adding extra backslashes to my URL strings?
NSDictionary *info = @{@"myURL":@"http://www.example.com/test"}; NSData data = [NSJSONSerialization dataWithJSONObject:info options:0 error:NULL]; NSString *string = [[NSString alloc] initWithData:policyData encoding:NSUTF8StringEncoding]; NSLog(@"%@", string);//{"myURL":"http:\/\/www.example.com\/test"}
I can strip the backslashes and use that string but I would like to skip that step if possible...
This worked for me
NSDictionary *policy = ....; NSData *policyData = [NSJSONSerialization dataWithJSONObject:policy options:kNilOptions error:&error]; if(!policyData && error){ NSLog(@"Error creating JSON: %@", [error localizedDescription]); return; } //NSJSONSerialization converts a URL string from http://... to http:\/\/... remove the extra escapes policyStr = [[NSString alloc] initWithData:policyData encoding:NSUTF8StringEncoding]; policyStr = [policyStr stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; policyData = [policyStr dataUsingEncoding:NSUTF8StringEncoding];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With