如何解析JSON中的所有数据?

时间:2018-09-06 02:47:09

标签: ios json swift alamofire

我解决的第一个问题

我正在使用Alamofire开发APIService,我尝试打印response并成功获取了数据,但是不幸的是,当我解析它时,JSON中的数据变成了 nil attendees对象。如何将json中的数据反映到参与者对象?

第二期

在完成所有调试之后,我解决了第一个问题。我使用的代码写在下面的答案中。我从JSON解析到attendees的数据,但是当我检查仅第一个数组被提取时。如何获取JSON中的所有数据?希望您能够帮助我。谢谢。

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 个答案:

答案 0 :(得分:1)

我解决了自己的问题。 :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)
                    }
                }

                }


        }

答案 1 :(得分:1)

分别使用循环来代替仅使用序列的 first 项的first

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

我建议使用Decodable

将JSON直接解码为结构