簡體   English   中英

如何解析JSON中的所有數據?

[英]How to parsed all the data in JSON?

我解決的第一個問題

我正在使用Alamofire開發APIService,嘗試打印response並成功獲取了數據,但是不幸的是,當我將其解析為attendees對象時,JSON中的數據變為nil 如何將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": ""
    },
  ]
]

我解決了自己的問題。 :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)
                    }
                }

                }


        }

而不是使用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直接解碼為結構

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM