gpt4 book ai didi

ios - 进行 JSON 序列化时出错 ErrorDomain Code=3840

转载 作者:行者123 更新时间:2023-11-28 15:09:08 25 4
gpt4 key购买 nike

我正在尝试删除 TableView 中的帖子,但在进行 JSON 序列化时出现错误,在这一行

let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]

如果是通过浏览器访问url,成功删除数据库中的数据,如果是从浏览器访问,也是这样返回json响应

{"message":"successfully deleted","result":1,"status":"Image has been deleted from drive"}

但我得到一个错误,提示 json 文本不是以数组或对象开头(代码 =3840),但正如您在上面看到的,它是一个 json 字典

Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

enter image description here

这是我使用的完整代码。这里出了什么问题?谢谢

func deletePost(_ indexPath: IndexPath) {

let tweet = tweetsArray[indexPath.row]
let uuid = tweet["uuid"] as! String
let imagePath = tweet["imagePath"] as! String

let url = URL(string: "http://localhost/Twitter/post.php")
var request = URLRequest(url: url!)
request.httpMethod = "POST"
let body = "uuid=\(uuid)&path=\(imagePath)"
request.httpBody = body.data(using: String.Encoding.utf8)

let session = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) in

if error == nil {

do {

let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]

guard let parsedJSON = json else {
print("error while parsing JSON")
return
}

let jsonMessage = parsedJSON["message"]
if jsonMessage != nil {
// hilangkan data di array
self.tweetsArray.remove(at: indexPath.row)
self.imagesArray.remove(at: indexPath.row)

// hilangkan rownya pada table view
self.tableView.deleteRows(at: [indexPath], with: .automatic)
self.tableView.reloadData()
}
}
catch {

// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error)"
self.showAlert(alertTitle: "sorry", alertMessage: message, actionTitle: "OK")
})
return
}
}
else {

// get main queue to communicate back to user
DispatchQueue.main.async(execute: {
let message = "\(error!.localizedDescription)"
self.showAlert(alertTitle: "sorry", alertMessage: message, actionTitle: "OK")
})
return
}
}
task.resume()
}

最佳答案

您可以尝试打印出您的服务器响应。请像这样更改 catch block 中的代码。并识别服务器端是否有错误。

您的服务器数据是正确的 json 格式然后打印出您的服务器数据并检查服务器数据是否有效。

URLSession.shared.dataTask(with: url) { (data, response, error) in

if let jsonData = data {
do {
let parsedData = try JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) as! [String: AnyObject]
if let area = parsedData["AREA"] as? [[String: AnyObject]] {
for a in area {
print(a["area_name"])
print(a["price"])
}
}
}
catch let err{
print("\n\n===========Error===========")
print("Error Code: \(error!._code)")
print("Error Messsage: \(error!.localizedDescription)")
if let data = data, let str = String(data: data, encoding: String.Encoding.utf8){
print("Server Error: " + str)
}
debugPrint(error)
print("===========================\n\n")

debugPrint(err)
}
}
else {
debugPrint(error as Any)
}

}.resume()

关于ios - 进行 JSON 序列化时出错 ErrorDomain Code=3840,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47950757/

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