【问题标题】:How to parsed all the data in JSON?如何解析 JSON 中的所有数据?
【发布时间】:2018-09-06 02:47:09
【问题描述】:

我解决的第一个问题

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

第二期

在进行了所有调试之后,我解决了第一个问题。我使用的代码写在下面的答案中。我将来自 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": ""
    },
  ]
]

【问题讨论】:

    标签: ios json swift alamofire


    【解决方案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 直接解码为结构体

    【讨论】:

    • 好的,知道了。非常感谢。 :)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多