在iOS中执行JSON序列化时失败,但是当通过浏览器访问API时,它是可以的

问题描述 投票:1回答:1

我正在尝试从我自己的API检索数据,如果我尝试使用chrome浏览器连接到我的API,它会像这样返回JSON数据

{“id”:“52”,“username”:“aasad23”,“fullname”:“aasad laksana”,“email”:“[email protected]”,“avatar”:“/ Applications / XAMPP / xamppfiles /的htdocs /微博/头像/ 52 / avatar.jpg“}

但是,当我尝试通过我的iOS应用程序访问API时,它在执行JSON序列化时出错,它会出错:数据无法读取,因为它的格式不正确

这是什么意思正确的格式?

我已经检查过这一行中的代码错误

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

这就是为什么激活catch错误并给出错误信息。

这是此任务的完整代码

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




            DispatchQueue.main.async(execute: {

                if error == nil {



                    do {

                        // json containes $returnArray from php
                        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                        // declare new parseJSON to store json
                        guard let parsedJSON = json else {
                            print("Error while parsing")
                            return
                        }


                        print(parsedJSON)

                        // get id from $returnArray["id"] in PHP - parseJSON["id"]
                        let id = parsedJSON["id"]

                        // successfully uploaded
                        if id != nil {

                            // save user information yang berasal dari server
                            UserDefaults.standard.set(parsedJSON, forKey: "parsedJSON")



                        } else {

                            // get main queue to communicate back to user
                            DispatchQueue.main.async(execute: {
                                let message = parsedJSON["message"] as! String
                                self.showAlert(alertTitle: "opppps", alertMessage: message, actionTitle: "OK")
                            })

                        }



                        //  JSON serialization error
                    } catch {

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

                    }

                    // error when connecting to server
                } else {

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

                }


            })

            }.resume()


    }
php ios json
1个回答
1
投票

尝试

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

而不是

try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
© www.soinside.com 2019 - 2024. All rights reserved.