sách gpt4 ai đã đi

Tại sao tệp JSON tôi tải xuống không còn là JSON nữa?

In lại 作者:行者123 更新时间:2023-11-28 11:41:21 29 4
mua khóa gpt4 Nike

我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是代码:

let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

documentsURL.appendPathComponent("analysis."+pathExtension)
return (documentsURL, [.removePreviousFile])
}

Alamofire.download("http://www...../analysis.json", to: destination).response { response in
if response.destinationURL != nil {
print(response.destinationURL!)

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]

let path = documentsDirectory + "/analysis.json"

if JSONSerialization.isValidJSONObject(path) {
print("Valid Json")
} khác {
print("InValid Json") ///// I am getting here "INValid Json" - how is that possible????
}
}
}

1 Câu trả lời

bởi vì con đường 是一个字符串,指示文件在您系统上的位置,类似于 file://path/to/analysis.json。这显然不是有效的 JSON。

您要检查的是该文件的nội dung 是否为有效 JSON。试试这个:

Alamofire.download("http://www...../analysis.json", to: destination).response { response in
guard detinationURL = response.destinationURL else { return }
guard data = Data(contentsOf: destinationURL) else { return }

LÀM {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
} catch {
print("InValid Json")
}
}

旁注:为什么不使用 Decodable?

关于json - 为什么我下载的 JSON 文件不再是 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53576005/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com