how to prevent NSJSONSerialization from adding extra escapes in URL

前端 未结 5 720

How do I prevent NSJSONSerialization from adding extra backslashes to my URL strings?

NSDictionary *info = @{@\"myURL\":@\"http://www.example.com/test\"};
NS         


        
5条回答
  •  庸人自扰
    2020-11-29 09:46

    I had this issue and resolved it by instead using the now available JSONEncoder. Illustrated with code:

    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)
    

提交回复
热议问题