Search code examples
objective-cnsjsonserialization

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


Solution

  • Yeah, this is quite irritating and even more so because it seems there's no "quick" fix to this (i.e. for NSJSONSerialization)

    source:
    http://www.blogosfera.co.uk/2013/04/nsjsonserialization-serialization-of-a-string-containing-forward-slashes-and-html-is-escaped-incorrectly/
    or
    NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly


    (just shooting in the dark here so bear with me)
    If, you're making your own JSON then simply make an NSData object out of a string and send it to the server.
    No need to go via NSJSONSerialization.

    Something like:

    NSString *strPolicy = [info description];
    NSData *policyData = [strPolicy dataUsingEncoding:NSUTF8StringEncoding];
    

    i know it won't be so simple but... hm... anyways