繁体   English   中英

使用Codable序列化为JSON时Swift String转义

[英]Swift String escaping when serializing to JSON using Codable

我正在尝试将我的对象序列化如下:

import Foundation

struct User: Codable {
    let username: String
    let profileURL: String
}

let user = User(username: "John", profileURL: "http://google.com")

let json = try? JSONEncoder().encode(user)

if let data = json, let str = String(data: data, encoding: .utf8) {
    print(str)
}

但是在macOS上我得到以下内容:

{"profileURL":"http:\/\/google.com","username":"John"}

(注意转义'/'字符)。

在Linux机器上,我得到:

{"username":"John","profileURL":"http://google.com"}

如何让JSONEncoder返回未转义的?

我需要JSON中的字符串严格未转义。

我最终使用replacingOccurrences(of:with:) ,这可能不是最好的解决方案,但它解决了这个问题:

import Foundation

struct User: Codable {
    let username: String
    let profileURL: String
}

let user = User(username: "John", profileURL: "http://google.com")

let json = try? JSONEncoder().encode(user)

if let data = json, let str = String(data: data, encoding: .utf8)?.replacingOccurrences(of: "\\/", with: "/") {
    print(str)
    dump(str)
}

我知道了。 事情是它没有任何\\字符。 它只是swift的属性,它总是会在控制台上返回这样一个字符串。 解决方法是j-son解析它。

不过,您可以在下面使用“/”字符串替换'\\ /'的解决方案中使用

 let newString = str.replacingOccurrences(of: "\\/", with: "/") 
 print(newString)

适用于iOS 13+ / macOS 10.15+

您可以使用.withoutEscapingSlashes选项到json解码器以避免转义斜杠

let user = User(username: "John", profileURL: "http://google.com")

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .withoutEscapingSlashes
let json = try? jsonEncoder.encode(user)

if let data = json, let str = String(data: data, encoding: .utf8) {
    print(str)
}

控制台O / P.

{“profileURL”:“ http://google.com ”,“用户名”:“John”}


注:由于提马丁- [R在评论\\/是一个有效的JSON转义序列。

在JSONEncoder / JSONDecoder中玩游戏时,我发现编码 - >解码时URL类型是有损的。

使用字符串初始化,相对于另一个URL。

init?(string: String, relativeTo: URL?)

可能会帮助这个苹果文档: https//developer.apple.com/documentation/foundation/url

但是,使用PropertyList版本:

let url = URL(string: "../", relativeTo: URL(string: "http://google.com"))! 
let url2 = PropertyListDecoder().decode([URL].self, from: PropertyListEncoder().encode([User]))

另一种方式

let url = URL(string: "../", relativeTo: URL(string: "http://google.com"))! 
let url2 = JSONDecoder().decode([URL].self, from: JSONEncoder().encode([User]))

希望对你有所帮助!!

实际上你不能这样做,因为在macOS和Linux中有一些不同的转义系统。 在linux //上是允许的,macOS - 不是(它使用NSSerialization)。 因此,您可以在字符串上添加百分比编码,这可以保证您在macOS和linux上使用相同的字符串,将字符串发布到服务器并正确验证。 在添加转义百分比时设置CharacterSet.urlHostAllowed 可以这样做:

init(name: String, profile: String){
        username = name
        if let percentedString = profile.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed){
            profileURL = percentedString
        }else{
            profileURL = ""
        }
    }

以同样的方式,你可以删除PercentEncoding和你不需要修改服务器端!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM