search

Home  >  Q&A  >  body text

objective-c - iOS中使用NSSerialization把对象转为JSON字符串后,多出来反斜杠的问题

代码

   NSDictionary *dic = @{@"url": @"http://..."};                                                                                                                                             
   NSLog(@"%@", dic);
   NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
   NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   NSLog(@"%@", jsonString);

执行结果:

2014-06-12 14:44:19.427 main[64877:1322484] {                                                                                                                                              
     url = "http://...";                                                                                                                                                                    
 }                                                                                                                                                                                          
 2014-06-12 14:44:19.429 main[64877:1322484] {                                                                                                                                              
   "url" : "http:\/\/..."                                                                                                                                                                   
 }                         

转换后的json字符串中url地址被转义了 :(

使用字符串替换可以事后弥补:

[jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];

如何事先预防呢?

PS:在和UIWebView进行js调用时需要不转义的json字符串,所以还是希望正面解决掉。

标题文字

迷茫迷茫2768 days ago909

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-22 09:01:59

    This does not need to be processed, just use it directly.
    If it is taken out directly and displayed on the Label, it will be escaped internally. So don't bother.
    You can refer to the blog I wrote "IOS 7 uses the system's own library to perform POST JSON asynchronous access operations"

    reply
    0
  • PHPz

    PHPz2017-04-22 09:01:59

    About 是否应该转义成/, 我并没细究. 但是很多开源实现 decode 的时候是不会将/转回 in the json standard document. Therefore, I don’t agree with the point of view mentioned above. In some cases, it must be processed, but I don’t know the official NSJSONSerialization processing method, so I switched to the open source jsonkit to bypass this problem

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-22 09:01:59

    Apple is so capricious, please replace it manually, similar to:

    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];
    

    See:
    how to prevent NSJSONSerialization from adding extra escapes in URL
    NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

    reply
    0
  • Cancelreply