简体   繁体   English

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

[英]Parsing JSON, data turns Nil in API Service Using Alamofire

I was about to get data from API, when I tried to print the response from the API, it printed successfully, and when I tried to print the data from an Object, it also printed successfully, but when I checked the variable attendees in my APIService , the value is always nil ,that's why when passing the value in UI it also turns into nil . 我正要从API获取数据,当我尝试从API打印响应时,它成功打印,而当我尝试从Object中打印数据时,它也成功打印,但是当我检查自己的变量参与者APIService ,该值始终为nil ,这就是为什么在UI中传递该值时也变为nil的原因 How can I get the data from JSON to reflect to attendess in Api Service. 如何从JSON获取数据以反映给Api Service中的出席者 I am too puzzled regarding this. 我对此感到困惑。 Please help me. 请帮我。 Thank you. 谢谢。

getParticipant Function in API Service 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)
            }
        }
    }

}

attendees in UI which should accept data also turns into nil because attendees in APIService is nil UI中应接受数据的参与者也变为nil,因为APIService中的参与者为nil

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
        }
    }

Sample JSON 样本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": ""
        },

Because of your Attendees model's data type is mismatched 由于您的与会者模型的数据类型不匹配

  • You should check every parameter's datatype of response and compare with your created Attendees datatype 您应该检查每个参数的响应数据类型,并将其与创建的Attendees数据类型进行比较

I hope this will work because of I faced this problem and solved it 我希望这会成功,因为我遇到了这个问题并已解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM