Ios Can';无法从Swift 3中的JSON获取指定值

Ios Can';无法从Swift 3中的JSON获取指定值,ios,json,swift3,Ios,Json,Swift3,快速解释我在做什么,有一个登录屏幕,用户键入一封电子邮件,然后弹出一个验证viewController,后端向用户的电子邮件发送验证代码,如果用户的电子邮件+验证码匹配,那么他将成功登录,后端将使用用户的完整信息+唯一ID进行响应。因此我想单独获取该唯一ID。 我的验证viewController代码: @IBAction func doneBtnPressed(_ sender: Any) { let request = NSMutableURLRequest(url: NSURL(

快速解释我在做什么,有一个登录屏幕,用户键入一封电子邮件,然后弹出一个验证viewController,后端向用户的电子邮件发送验证代码,如果用户的电子邮件+验证码匹配,那么他将成功登录,后端将使用用户的完整信息+唯一ID进行响应。因此我想单独获取该唯一ID。

我的验证viewController代码:

@IBAction func doneBtnPressed(_ sender: Any) {

    let request = NSMutableURLRequest(url: NSURL(string: "http://anydomain.com/verif.php")! as URL)
    request.httpMethod = "POST"
    let postString = "bdemail=\(bdEmail)&verifcode=\(verifyCodeTxtField.text!)"
    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("error=\(error)")
            return
        }else {
            do {
                if let data = data,
                    let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
                    let users = json["BD_Id"] as? [[String: Any]] {
                    print(users)
                }
            } catch {
                print("Error deserializing JSON: \(error)")
            }
        }

        print("response = \(response)")

        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(responseString)")
    }

    task.resume()

    self.performSegue(withIdentifier: "mySegueIdentifier", sender: nil)

}
但是我只得到完整的响应,我想单独获得唯一的ID-->“BD_ID”,这样我就可以将它存储起来,在我的应用程序中使用它。

以下是答复:

    { URL: http://anydomain.com/verif.php } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Tue, 07 Feb 2017 07:40:00 GMT";
    Server = Apache;
    "Transfer-Encoding" = Identity;
} })


responseString = Optional(  [{"Rcode":101,"BD_Id":"8","BD_Name":"","BD_Email":"email@domain.com","BD_Country":"","BD_Home_Lat":"","BD_Home_Lon":"","BD_BT":"","BD_RH":"","BD_DOB":"0000-00-00","BD_Mobile":"","BD_Last_Donation":"0000-00-00","BD_Token":""}])
ID没有打印出来,那我错在哪里?

提前谢谢。请注意,我是Swift网络的初学者,这是我第一次使用JSON

更新1:

我已经尝试了下面的代码,但仍然不起作用,并且print语句没有出现,这意味着数据是空的| | nil,但是如果是这样,那么最后一个print语句将如何打印整个JSON脚本

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

        if error != nil {
            print("error=\(error)")
            return
        }else {
            do {
                if data != nil {
                    print("Hello from the inside...")
                    let json = try JSONSerialization.jsonObject(with: data!) as? [[String: Any]]
                    for obj in json! {
                        if let bdId = obj["BD_Id"] as? String {
                            print(bdId)
                        } 
                    }
                }
            } catch {
                print("Error deserializing JSON: \(error)")
            }
        }

        print("response = \(response)")

        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(responseString)")
    }

试试这个。。。JSON是一个字典数组<代码>[[String:Any]]

  do{
         let json = try JSONSerialization.jsonObject(with: data!, options:[]) as! [[String:Any]]
          for obj in json {
               if let bdId = obj["BD_Id"] as? String {
                   print(bdId)
               } 
          }
  }
  catch {
      print("Error with Json: \(error)")
  }

试试这个。。。JSON是一个字典数组<代码>[[String:Any]]

  do{
         let json = try JSONSerialization.jsonObject(with: data!, options:[]) as! [[String:Any]]
          for obj in json {
               if let bdId = obj["BD_Id"] as? String {
                   print(bdId)
               } 
          }
  }
  catch {
      print("Error with Json: \(error)")
  }

您将三个“let”测试放在一行/条件中。打破它们,了解它没有通过的测试。JSON是一个顶级数组,包含一个对象和一个字典。所以
json
应该是
[[String:Any]]
。然后从它开始,您的ID应该是
json[0][“BD_ID”]
不相关,但在Swift 3中,将
request
行替换为
var request=URLRequest(url:url(字符串):http://anydomain.com/verif.php)
并将
作为URL请求删除。这两种编码都可以简化为
。utf8
将三个“let”测试放入一行/条件中。打破它们,了解它没有通过的测试。JSON是一个顶级数组,包含一个对象和一个字典。所以
json
应该是
[[String:Any]]
。然后从它开始,您的ID应该是
json[0][“BD_ID”]
不相关,但在Swift 3中,将
request
行替换为
var request=URLRequest(url:url(字符串):http://anydomain.com/verif.php)
并将
作为URL请求删除。这两种编码都可以简化为
.utf8