Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent NSJSONSerialization from adding extra escapes in URL

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...

like image 501
joels Avatar asked Oct 29 '13 06:10

joels


1 Answers

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]; 
like image 151
joels Avatar answered Sep 27 '22 18:09

joels