gpt4 book ai didi

swift - 为什么 JSON 序列化省略了 JSON 字符串中的正斜杠?

转载 作者:行者123 更新时间:2023-11-28 06:02:11 39 4
gpt4 key购买 nike

来自 MockURL200Session 的传入 JSON 生成值 2017-11-25 09:47:18 Etc/GMT

class MockURL200Session: URLSessionProtocol {
var nextDataTask = MockURLSessionDataTask()
var nextData: Data?
var nextError: Error?

func httpURLResponse(request: NSURLRequest) -> URLResponse {
return HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: nil)!
}

func dataTask(with request: NSURLRequest, completionHandler: @escaping DataTaskResult) -> URLSessionDataTaskProtocol {
let json = ["expires_date": "2017-11-25 09:47:18 Etc/GMT"]
nextData = try! JSONSerialization.data(withJSONObject: json, options: [])
completionHandler(nextData, httpURLResponse(request: request), nextError)
return nextDataTask as URLSessionDataTaskProtocol
}
}

但是,一旦到达客户端,正斜杠就会消失并变为 2017-11-25 09:47:18 Etc GMT

self.httpClient.post(url: url) { (data, response, error) in
let json = try! JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<String, Any>
}

我该如何防止或避免它?

最佳答案

dataTask() 中,您对 JSONSerialization.data() 的调用在解码时会转义正斜杠。在从 dataTask() 返回之前,您可以在 nextData 中看到它。在返回客户端的过程中,\/ 组合被删除了。我猜是在您尝试解码服务器响应时。现在您知道发生了什么,您可以找到解决其余问题的方法 here ,或者如果没有,谷歌类似“swift jsonserialization escape slash”之类的东西。祝你好运。

关于swift - 为什么 JSON 序列化省略了 JSON 字符串中的正斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308664/

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