简体   繁体   中英

How to parsed all the data in JSON?

First issue I addressed

I am working on an APIService using Alamofire, I tried to print the response and the I got the data successfully, but unfortunately the data from JSON turns to nil when I parse it to the attendees Object. How can I reflect the data from json to the attendees object?

Second Issue

I solved the 1st issue, after all the debugging I had. The codes I used was written in my answer below. I parsed data from JSON going to attendees but as I checked only the first array was fetch. How can I get all the data inside the JSON? Hope you can help me. Thank you.

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

}

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": ""
    },
 {
        "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": ""
    },
  ]
]

I solved my own issue. :D

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

        if let error = response.error
        {
            failureBlock(error)
            return
        }
        if let jsonDictionary = response.result.value as? [Dictionary<String, Any>]{
            if let eventparticipants = jsonDictionary.first {
                print(eventparticipants)
                if let partObj = eventparticipants["event_participants"] as? [[String : Any]]{
                    let attendeeObj = partObj.first
                    let attendees = Attendees.init(JSON: attendeeObj!)
                        successBlock(attendees)
                    }
                }

                }


        }

Instead of using first which gets only the first item of the sequence use a loop respectively.

if let events = response.result.value as? [[String : Any]] {
    for event in events {
        if let eventparticipants = event["event_participants"] as? [[String : Any]] {
            print(eventparticipants)
            for participant in eventparticipants {
                let attendees = Attendees.init(JSON: participant)
                successBlock(attendees)
            }
       }
    }
}

I recommend to decode the JSON directly into structs with Decodable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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