[英]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. 我正在使用Alamofire开发APIService,尝试打印
response
并成功获取了数据,但是不幸的是,当我将其解析为attendees
对象时,JSON中的数据变为nil 。 How can I reflect the data from json to the attendees object? 如何将json中的数据反映到与会者对象?
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. 我从JSON解析了要发送给
attendees
数据,但是当我检查到只有第一个数组被提取时。 How can I get all the data inside the JSON? 如何获取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 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": ""
},
]
]
I solved my own issue. 我解决了自己的问题。 :D
: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)
}
}
}
}
Instead of using first
which gets only the first item of the sequence use a loop respectively. 而不是使用
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)
}
}
}
}
I recommend to decode the JSON directly into structs with Decodable
我建议使用
Decodable
将JSON直接解码为结构
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.