Php Swift 3解析json响应时出现问题

Php Swift 3解析json响应时出现问题,php,json,xcode,swift3,Php,Json,Xcode,Swift3,我是Swift新手,但我一直在学习一些示例,比如如何让登录应用程序将用户名和密码字段发送到php文件,并返回json响应 当我打印我的回复时,我得到: [{“userid”:1,“username”:“rodrigo”,“password”:“minhoca”,“groupname”:“couple”}] 但是,当我试图解析json时,我永远无法设置username变量,因为我永远不会进入代码的这一部分,我只会得到“here” 谢谢你的帮助 func sendLoginInfo(username

我是Swift新手,但我一直在学习一些示例,比如如何让登录应用程序将用户名和密码字段发送到php文件,并返回json响应

当我打印我的回复时,我得到:

[{“userid”:1,“username”:“rodrigo”,“password”:“minhoca”,“groupname”:“couple”}]

但是,当我试图解析json时,我永远无法设置username变量,因为我永远不会进入代码的这一部分,我只会得到“here”

谢谢你的帮助

func sendLoginInfo(username: String, password: String) -> String{
        if let url = URL(string: "myphpurl"){
            let request = NSMutableURLRequest(url:url)
            request.httpMethod = "POST";// Compose a query string
            let postString = "?username=\(myUsername)&password=\(myPassword)"
            request.httpBody = postString.data(using: String.Encoding.utf8)
            let task = URLSession.shared.dataTask(with:request as URLRequest){
                data, response, error in

                if error != nil{
                    print("1\(error)")
                }
                else{
                    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                  print("response string = \(responseString!)")
                }

                do {

                    if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {

                        // Print out dictionary
                        print(convertedJsonIntoDict)

                        // Get value by key
                        let firstNameValue = convertedJsonIntoDict["username"] as? String
                        print("here = \(firstNameValue!)")

                    }
                    else{
                         print("here")
                    }
                } catch let error as NSError {
                    print(error.localizedDescription)
                }
            }
            task.resume()
        }
        return ""
    }

在代码中将NSDictionary更改为NSArray,因为您将获得一个数组并尝试转换为Dictionary:

 if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray 
获取索引0处的对象,它将为您提供命令&然后您可以获取用户名

因此,最终代码将是:

func sendLoginInfo(username: String, password: String) -> String{
    if let url = URL(string: "myphpurl"){
        let request = NSMutableURLRequest(url:url)
        request.httpMethod = "POST";// Compose a query string
        let postString = "?username=\(myUsername)&password=\(myPassword)"
        request.httpBody = postString.data(using: String.Encoding.utf8)
        let task = URLSession.shared.dataTask(with:request as URLRequest){
            data, response, error in

            if error != nil{
                print("1\(error)")
            }
            else{
                let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
              print("response string = \(responseString!)")
            }

            do {

                if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray {

                    // Print out dictionary
                    print(convertedJsonIntoDict)

                    // Get value by key
                    let firstNameValue = (convertedJsonIntoDict[0] as! NSDictionary)["username"] as? String
                    print("here = \(firstNameValue!)")

                }
                else{
                     print("here")
                }
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
        task.resume()
    }
    return ""
}

谢谢,但现在它给了我:使用未声明的类型NSDictionarySorry我在代码中输入了一个错误。请重试此代码,我已对其进行了编辑。我把关键字拼错了,这是字典