gpt4 book ai didi

json - 如果 json 数据包含新行 ("\n",Swift 4 无法正确解码)

转载 作者:可可西里 更新时间:2023-11-01 01:07:56 25 4
gpt4 key购买 nike

<分区>

如果 json 数据包含新行(“\n”),Swift 4 无法正确解码。我能为这种情况做些什么。请看一下我的示例代码:

var userData = """
[
{
"userId": 1,
"id": 1,
"title": "Title \n with newline",
"completed": false
}
]
""".data(using: .utf8)

struct User: Codable{
var userId: Int
var id: Int
var title: String
var completed: Bool
}

do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
let model = try decoder.decode([User].self, from:userData!) //Decode JSON Response Data
print(model)
} catch let parsingError {
print("Error", parsingError)
}

如果我像下面那样更改 userData 值,那么它可以正确解码。

var userData = """
[
{
"userId": 1,
"id": 1,
"title": "Title \\n with newline",
"completed": false
}
]
""".data(using: .utf8)

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com