【问题标题】:how to prevent NSJSONSerialization from adding extra escapes in URL如何防止 NSJSONSerialization 在 URL 中添加额外的转义
【发布时间】:2013-11-08 04:22:34
【问题描述】:

如何防止 NSJSONSerialization 在我的 URL 字符串中添加额外的反斜杠?

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"}

我可以去掉反斜杠并使用该字符串,但如果可能,我想跳过该步骤...

【问题讨论】:

  • 你找到解决方案了吗?
  • 这是我在 AFJSONRequestSerializer 上的一个类别的答案,基于 joel 的方法:stackoverflow.com/a/30802624/1675788
  • 如果有人在调试器中看到这个,它可能不是你想象的那样。 lldb 将在显示和打印字符串时转义字符串中的某些字符。进行测试,而不是做po <string>po print(<string>)。我失去了 3 个小时的生命。 “\”实际上并不存在......
  • @BTRUE ...而您,先生,是个铁石心肠的导弹人。你刚刚为我节省了 3 个小时。非常感谢。

标签: objective-c nsjsonserialization


【解决方案1】:

这对我有用

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

【讨论】:

  • 这会损坏像 \/\/ 笑脸这样的字符串。 “Here is a backslash after slash: \\/”将替换为“Here is a backslash after \\/”,这不是任何人想要的。
  • @LevWalkin "\/\/" 微笑将被编码为 "\\/\\/",将 "\/" 替换为 "/" 后将是 "'\/\/"再次
【解决方案2】:

是的,这很烦人,甚至更烦人,因为似乎没有“快速”解决方法(即用于 NSJSONSerialization)

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

NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly


(这里只是在黑暗中拍摄,所以请耐心等待)
如果您正在制作自己的 JSON,那么只需从字符串中制作一个 NSData 对象并将其发送到服务器。
无需通过 NSJSONSerialization。

类似:

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

我知道这不会那么简单,但是……嗯……无论如何

【讨论】:

  • 感谢您的链接。至于在黑暗中拍摄,如果我手动构建 json 字符串,我会这样做,但我使用 NSJSONSerialization 使构建字符串更加容易。以上只是问题的一个简单示例。
  • 是的,有更多的变量和参数要考虑,我可以理解 NSJSONSerialization 更容易。无论如何,如果我找到解决方案,我会记得在这里发帖。
  • 最后,我也只是为这种情况手动构建了 JSON,在所有其他情况下,我都使用 JSONSerializer。不喜欢让应用程序返回并替换字符串,因为它可能会取消这些字符的有效出现(它们可能在 Base64 编码中自然发生)。
【解决方案3】:

如果您的目标是 >= iOS 13.0,那么只需在选项中添加 .withoutEscapingSlashes。

例子:

let data = try JSONSerialization.data(withJSONObject: someJSONObject, options: [.prettyPrinted, .withoutEscapingSlashes])

print(String(data: data, encoding: String.Encoding.utf8) ?? "")

【讨论】:

    【解决方案4】:

    我已经跟踪这个问题很多年了,但仍然没有解决。我相信苹果永远不会因为遗留原因修复它(它会破坏东西)。

    Swift 4.2 中的解决方案:

    let fixedString = string.replacingOccurrences(of: "\\/", with: "/")
    

    它会将所有\/ 替换为/,这样做是安全的。

    【讨论】:

      【解决方案5】:

      我遇到了这个问题,并通过使用现在可用的 JSONEncoder 解决了它。用代码说明:

      struct Foozy: Codable {
          let issueString = "Hi \\ lol lol"
      }
      
      let foozy = Foozy()
      
      // crashy line
      //let json = try JSONSerialization.data(withJSONObject: foozy, options: [])
      
      // working line
      let json = try JSONEncoder().encode(foozy)
      

      【讨论】:

        猜你喜欢
        • 2021-08-24
        • 1970-01-01
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        • 2022-09-30
        • 2013-04-05
        • 2014-08-15
        • 1970-01-01
        相关资源
        最近更新 更多