asked    Cleveland     2018-10-22       ios       76 view        2 Answers

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

  2 Answers  

        answered    Mavis     2018-10-22      

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



        answered    Devin     2018-10-22      

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

                }


        }




Your Answer





 2018-10-22         Pamela

Cloning tkinter Buttons with other function

right now I am working on a python phone book with following layout:|Name | Surname | Now I want to create a button for each contacts Name and Surname, so 2 buttons for 1 contact * n. If I press on the Name or Surname a box with more details of the contact should open.Can anyone please help me?I tried following so far:prename = ["John", "Jack"]surname = ["Tompson", "Daniels"]x = 0y = 0for pn in prename: pre = Button(main, text=pn) pre.grid(row=x, column=0) x += 1for sn in surname: sur = Button(main, text=sn) sur.grid(row=y, column=1) y += 1 You need to ...
 python                     1 answers                     53 view
 2018-10-22         York

Facing problem with overrideredirect(True) method in tkinter on debian based Linux

I am developing an GUI using python tkinter in debian based linux.Whenever I am removing root and toplevel window title bar using overrideredirect(True) method I am facing below mentioned problems.Entry field not working(not getting focus/not getting input) when I am clicking on it.Whenever I use Alt+Tab key then only it get focused.Then I have to keep pressed mouse left or right button on clicked position then only entry field accept input otherwise input is written on terminal window.Could any one suggest an alternative way to remove titlebar or to hide title bar in tkin...
 tk                     1 answers                     53 view
 2018-10-22         Tammy

Using returned arrays

I am trying to run a procedure that takes an array called ds_out, changes values of ds_out(0) and ds(1) to either 0 or 1 depending on the state of a checkbox and returns it. I want to then output the values after the procedure but it looks like the procedure is not returning the array and printing the values in the initialising array. If you have the puts lines in the procedure it works.I've looked at tutorials and examples but I don't understand them. I want the basic of basic examples but can't find them.Below is the code I have used:global ds_outarray set ds_out {0 01 ...
 tcl                     1 answers                     52 view