如何在没有 unicode 转义序列 Swift 的情况下获得 JSON 响应
How to get JSON response without unicode escape sequence Swift
我正在尝试以正确的形式获取数据,这是我的请求示例:
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.allHTTPHeaderFields = [
"Content-Type": "http",
"Authorization": token
]
let session = URLSession.shared
session.dataTask(with: request) { data, response, error in
guard let data = data else {
completion(.failure(.invalidData))
return
}
do {
if let string = String(data: data, encoding: .utf8) { print(string) }
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
completion(.failure(.invalidURL))
}
catch {
completion(.failure(.decodingProblem))
}
}.resume()
在那个请求之后,我总是得到 JSON 结果,就像下一个一样。
但我想得到没有unicode序列的正确文本
{
"result": {
"id": 2,
"write_date": "2021-10-05T08:46:53.945082",
"name": "\u041e\u0432\u0430\u0434\u0447\u0443\u043a",
"x_phone": "+380999999999",
"number_policy": "\u0422\u0435\u0441\u0442",
"client_email": "test@it.com.ua",
"x_sk_phone1": "+380999999999",
"x_manager1": "\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 1",
"x_u_phone": "45454544",
"x_u_email": "app@it.com.ua",
"x_u_phone_man1": "123456789",
"x_street": "\u0456\u0432\u0441\u044c\u043a\u0430, 29",
"x_street2": "\u043b\u0456\u0432\u0441\u044c\u043a\u0438\u0439\u00bb, 5 \u044d\u0442\u0430\u0436",
"x_city": "\u041a\u0438\u0457\u0432",
"x_com_street": "\u043e\u0447\u0438\u0446\u044c\u043a\u0430,44",
"x_ost_zab": "\u0422\u0430\u043a",
"x_zab_v_obos": "\u0422\u0430\u043a",
"x_zab_v_rem": "\u041d\u0456",
"x_travma": "\u0422\u0430\u043a",
"x_sd_1": "100%",
"x_sd_2": "\u0422\u0430\u043a",
"x_sd_3": "\u0422\u0430\u043a",
"x_sp_1": "\u0422\u0430\u043a, \u0432 \u043c\u0435\u0436\u0430\u0445 \u043b\u0456\u043c\u0456\u0442\u0443",
"x_dop_1": "\u0422\u0430\u043a",
"x_info": "\u0422\u0435\u0441\u0442\u043e\u0432\u0438\u0439 \u0437\u0430\u043f\u0438\u0441 \u0434\u043b\u044f \u0432\u0456\u0434\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043d\u044f \u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0456 \u043a\u043b\u0456\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u0431\u0456\u043b\u044c\u043d\u043e\u043c\u0443 \u0434\u043e\u0434\u0430\u0442\u043a\u0443",
"x_limit_1": "300 000",
"x_limit_other": "Some"
}
}
所以我想获得没有 unicode 序列的正确文本。
您的 JSON 字符串有一些问题。使用 \U 时,它应该发送 4 个字节(8 个十六进制字符),但它只发送 2 个字节(4 个十六进制字符)。如果您可以控制正在 returned 的内容,最简单的解决方案是 return \u 而不是 \U,这是 2 个字节的正确 unicode 转义,否则您将不得不进行一些字符串操作并应用解码 json:
之前的字符串转换
let jsonData = Data(#"{"x_city":"\U041a\U0438\U0457\U0432"}"#.utf8)
let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
let cleanedData = Data(
jsonString
.replacingOccurrences(of: #"\U"#, with: #"\u"#)
.applyingTransform(.init("Hex-Any"), reverse: false)!
.utf8
)
struct Response: Codable {
let xCity: String
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode(Response.self, from: cleanedData)
print(response) // Response(xCity: "Київ")
} catch {
print(error)
}
我正在尝试以正确的形式获取数据,这是我的请求示例:
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.allHTTPHeaderFields = [
"Content-Type": "http",
"Authorization": token
]
let session = URLSession.shared
session.dataTask(with: request) { data, response, error in
guard let data = data else {
completion(.failure(.invalidData))
return
}
do {
if let string = String(data: data, encoding: .utf8) { print(string) }
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
completion(.failure(.invalidURL))
}
catch {
completion(.failure(.decodingProblem))
}
}.resume()
在那个请求之后,我总是得到 JSON 结果,就像下一个一样。 但我想得到没有unicode序列的正确文本
{
"result": {
"id": 2,
"write_date": "2021-10-05T08:46:53.945082",
"name": "\u041e\u0432\u0430\u0434\u0447\u0443\u043a",
"x_phone": "+380999999999",
"number_policy": "\u0422\u0435\u0441\u0442",
"client_email": "test@it.com.ua",
"x_sk_phone1": "+380999999999",
"x_manager1": "\u041c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 1",
"x_u_phone": "45454544",
"x_u_email": "app@it.com.ua",
"x_u_phone_man1": "123456789",
"x_street": "\u0456\u0432\u0441\u044c\u043a\u0430, 29",
"x_street2": "\u043b\u0456\u0432\u0441\u044c\u043a\u0438\u0439\u00bb, 5 \u044d\u0442\u0430\u0436",
"x_city": "\u041a\u0438\u0457\u0432",
"x_com_street": "\u043e\u0447\u0438\u0446\u044c\u043a\u0430,44",
"x_ost_zab": "\u0422\u0430\u043a",
"x_zab_v_obos": "\u0422\u0430\u043a",
"x_zab_v_rem": "\u041d\u0456",
"x_travma": "\u0422\u0430\u043a",
"x_sd_1": "100%",
"x_sd_2": "\u0422\u0430\u043a",
"x_sd_3": "\u0422\u0430\u043a",
"x_sp_1": "\u0422\u0430\u043a, \u0432 \u043c\u0435\u0436\u0430\u0445 \u043b\u0456\u043c\u0456\u0442\u0443",
"x_dop_1": "\u0422\u0430\u043a",
"x_info": "\u0422\u0435\u0441\u0442\u043e\u0432\u0438\u0439 \u0437\u0430\u043f\u0438\u0441 \u0434\u043b\u044f \u0432\u0456\u0434\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043d\u044f \u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0456 \u043a\u043b\u0456\u0435\u043d\u0442\u0430 \u0432 \u043c\u043e\u0431\u0456\u043b\u044c\u043d\u043e\u043c\u0443 \u0434\u043e\u0434\u0430\u0442\u043a\u0443",
"x_limit_1": "300 000",
"x_limit_other": "Some"
}
}
所以我想获得没有 unicode 序列的正确文本。
您的 JSON 字符串有一些问题。使用 \U 时,它应该发送 4 个字节(8 个十六进制字符),但它只发送 2 个字节(4 个十六进制字符)。如果您可以控制正在 returned 的内容,最简单的解决方案是 return \u 而不是 \U,这是 2 个字节的正确 unicode 转义,否则您将不得不进行一些字符串操作并应用解码 json:
之前的字符串转换let jsonData = Data(#"{"x_city":"\U041a\U0438\U0457\U0432"}"#.utf8)
let jsonString = String(data: jsonData, encoding: .utf8) ?? ""
let cleanedData = Data(
jsonString
.replacingOccurrences(of: #"\U"#, with: #"\u"#)
.applyingTransform(.init("Hex-Any"), reverse: false)!
.utf8
)
struct Response: Codable {
let xCity: String
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode(Response.self, from: cleanedData)
print(response) // Response(xCity: "Київ")
} catch {
print(error)
}