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

问题描述 投票:-1回答:2

第一期我提到了

我正在使用Alamofire开发一个APIService,我试图打印response并且我成功获得了数据,但不幸的是,当我将它解析为attendees对象时,来自JSON的数据变为零。如何将json中的数据反映给与会者对象?

第二期

在完成所有调试之后,我解决了第一个问题。我使用的代码写在下面的答案中。我从JSON解析数据到attendees但是我检查了第一个数组是fetch。如何获取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": ""
    },
  ]
]
ios json swift alamofire
2个回答
1
投票

而不是使用仅获得序列的第一项的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直接解码为结构


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

                }


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