从SwiftyJSON块访问值时遇到问题

时间:2017-03-11 18:53:32

标签: ios json swift swifty-json

所以这是我的ALamofire查询返回的JSON。

{
    "status": "",
    "minute": "5",
    "seconds": "54"
}

下面是我用来处理返回的JSON的函数。我无法从上面的JSON中提取分钟和秒值。我对分钟和秒值非常感兴趣。

func returnTime(){
    if let test22 = self.userdefaults.string(forKey: "fiveholdid"){
        Alamofire.request(ticketingDets,method:.post,parameters: ["command":"remaining","id":test22]).validate().responseString{
            responseData in
            switch responseData.result{

            case .success(let jJson):
               print(jJson)
                self.timerJSON = JSON(jJson)
                if let status =  self.timerJSON["status"].string{
                    //print("STATUS" + status)
                    if(status=="expired"){
                        print("In Timer Expired")
                        self.throwAlert(title: "RESERVATION", message: "Your Reservation has Expired. Please try again.", delay: 3.0)
                        self.userdefaults.removeObject(forKey: "fiveholdid")
                    }
                    }else{

                    var minute = self.timerJSON["minute"].stringValue
                    print(minute)
                    var second = self.timerJSON["seconds"].stringValue
                    print("second"+String(second))
                    var secondd = ""
                    if((second.characters.count) < 2){
                        var secondd = "0" + String(second)
                    }else{
                        var secondd = String(second)
                    }
                    self.timeremaining.text = String(minute)+":"+String(secondd)+" remaning"

                }

                //self.datesLabel.text = jJson

            case .failure(let error):
                print(error)
            }

        }
}

1 个答案:

答案 0 :(得分:0)

所以答案就是这样:

        Alamofire.request(ticketingDets,method:.post,parameters: ["command":"remaining","id":test22]).validate().responseString{

    Alamofire.request(ticketingDets,method:.post,parameters: ["command":"remaining","id":test22]).validate().responseJSON{

所以我不得不告诉AF我收到了JSON而不仅仅是一个字符串...