【问题标题】:get value of nested array alamofire swift 3获取嵌套数组alamofire swift 3的值
【发布时间】:2016-12-16 08:30:29
【问题描述】:

我需要获取嵌套数组值、用户数据和令牌数据。我需要这些数据,因为我需要初始化会话变量才能在我的应用程序中使用它们。

例如用户名fotografia,设法获取到主数组中的数据rc和msg,但是我无法获取嵌套数组的值

["rc": 00, 
"user": {
"__v" = 0;
deviceId = "";
email = "john@gmail.com";
fullName = "SMITH  JOHN ";
lastName = "SMITH ";
modifiedAt = "2016-12-16T06:08:58.856Z";
name = "JOHN ";
photo = "";
provider = "";
"provider_id" = "";
status = 01;
tel = 3333333333;
typeUser = USER;
username = "john@gmail.com";
}, "token": {
"__v" = 0;
"_id" = 585384e3ccc4;
createdAt = "2016-12-16T08:10:03.407Z";
userId = 585384e3ccc4;
value = Z4WedlAzhdkap;
}, "msg": success]




@IBAction func btnLogin(_ sender: Any) {
    let gsUtil=GSUtil()
    let user = txtUser.text!
    let password = txtPass.text!
    let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
    let base64Credentials = credentialData.base64EncodedString(options: [])
    let headers = ["Authorization": "Basic \(base64Credentials)"]
    Alamofire.request(gsUtil.getCompleteURI()+"user/auth/",
                      method: .post,
                      parameters: nil,
                      encoding: URLEncoding.default,
                      headers:headers)
        .validate()
        .responseJSON { response in
            if response.result.value != nil{
                let json = response.result.value as? [String: Any]
                let rc=json?["rc"]
                let msg=json?["msg"]
                if rc as? String=="00"
                {
                    self.showMessage(msg: msg as! String)
                }
            }else{
                self.showMessage(msg: "Error en los datos")
            }
    }
    }

【问题讨论】:

    标签: nested swift3 alamofire xcode8


    【解决方案1】:

    我真的不明白这个问题。你已经完成了你需要做的事情(只差一步):

    我会首先使用大量 if lets 来摆脱所有讨厌的选项:

    if let json = input as? [String: Any] {
        let msg = json["msg"]
        if let rc = json["rc"] as? String, rc == "00" {
            self.showMessage(msg: msg as! String)
        }
        if let user = json["user"] as? [String : Any] {
            print(user)
        }
        if let token = json["token"] as? [String : Any] {
            print(token)
        }
    } else {
        self.showMessage(msg: "Error en los datos")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多