美文网首页
iOS中对象转为JSON字符串后,多出来反斜杠的问题、后台请求返

iOS中对象转为JSON字符串后,多出来反斜杠的问题、后台请求返

作者: 缘來諟夢 | 来源:发表于2021-03-30 13:57 被阅读0次

代码

   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字符串,所以还是希望正面解决掉。

相关文章

网友评论

      本文标题:iOS中对象转为JSON字符串后,多出来反斜杠的问题、后台请求返

      本文链接:https://www.haomeiwen.com/subject/asxghltx.html