解析JSON,数据在使用Alamofire的API服务中变为Nil

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

我准备从API获取数据,当我尝试从API打印响应时,它成功打印,当我尝试从Object打印数据时,它也成功打印,但当我检查了我的变量与会者时APIService,值总是为零,这就是为什么在UI中传递值时它也变为零。如何从JSON获取数据以反映Api服务中的与会者。我对此感到困惑。请帮我。谢谢。

API服务中的getParticipant函数

func getParticipants(passcode: String,
                     participantType: ParticipantType,
                     successBlock: @escaping (Attendees?) -> Void,
                     failureBlock: @escaping (Error) -> Void)
{
    let attendeesURL = URL(string: "\(GET_PARTICIPANTS_URL)/\(passcode)/\(participantType)")

    Alamofire.request(attendeesURL!, method: .get).responseJSON { (response) in
        print(response)

        if let error = response.error
        {
            failureBlock(error)
            return
        }

        if let attendeeJSON = response.result.value as? [Dictionary<String, Any>],
            let attendeeObj = attendeeJSON.first {
            print(attendeeObj)
            let attendees = Attendees.init(JSON: attendeeObj)
            successBlock(attendees)
            }
        }
    }

}

UI中应该接受数据的与会者也会变为零,因为APIService中的与会者是零

var passcode = ""
var attendees = [Attendees]()

 @IBAction func submitButton(_ sender: UIButton) {

    //readvalues

    passcode = passcodeLabel.text!


    //check if empty

    if(passcode.isEmpty)
    {
        _ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please input valid event code.")
    } else {
        getParticipants()
    }

}

//GET PARTICIPANTS FUNCTION

func getParticipants() {

   // var participantType: ParticipantType!



    let api = APIService(APIKey: passcode)

    api.getParticipants(passcode: passcode, participantType: .all, successBlock: { (attendees) in


        if let attendeesArray = attendees {

            self.attendees = [attendeesArray]
            do {
            DispatchQueue.main.async{

                _ = SCLAlertView(appearance: appearance).showError("Message", subTitle: "Details:\(self.attendees)")

                return
            }

        }
        }

    }) { (error) in
        DispatchQueue.main.async{

            _ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "Network Error:\(error)")

            return
        }
    }

示例JSON

[
{
    "event_name": "Laugh Trip",
    "event_participants": [
        {
            "participant_id": "6f1e7fd5-6da9-4d5b-bc91-4771aeaa5235",
            "employee_number": "",
            "last_name": "name",
            "first_name": "name",
            "middle_name": "",
            "display_name": "name, name ",
            "department_name": "IT",
            "position_name": "Application Developer",
            "registered_flag": true,
            "registered_datetime": "2018-07-16T14:51:57.813",
            "registration_type": 1,
            "delete_flag": false,
            "manual_reg_flag": false,
            "out_flag": true,
            "out_datetime": "2018-07-16T14:54:00.000",
            "classification": 1,
            "others": ""
        },
ios json dictionary alamofire swift4.2
1个回答
0
投票

由于您的Attendees模型的数据类型不匹配

  • 您应该检查每个参数的响应数据类型,并与您创建的Attendees数据类型进行比较

我希望这会有效,因为我遇到了这个问题并解决了它

© www.soinside.com 2019 - 2024. All rights reserved.