Reputation: 174
I have a UITableview in which I need populate data from JSON
.
I'm using MVC Architecture. But I'm facing error in appending values to Model
class DisplayAttendanceModel { let name: String? let date: String? let subject: String? let status: String? init(name: String, date:String, subject:String, status:String) { self.name = name self.date = date self.subject = subject self.status = status }
var arrData = [DisplayAttendanceModel]()
let headers : HTTPHeaders = ["Authorization":"token" + " " + ProfileAuth]
let params : [String:Any] = [
"date":"15/07/2019",
"course_id":1,
"batch_id":1,
"subject_id":1
]
Alamofire.request(displayURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
if((response.result.value) != nil) {
var jsonVAr = JSON(response.result.value!)
if let da = jsonVAr["attendence"].arrayObject {
self.data = da as! [[String:AnyObject]]
print(self.data)
for item in self.data {
print(item["student__first_name"] ?? "NO")
var names = item["student__first_name"]?.stringValue
self.arrData.append(DisplayAttendanceModel(name: names ?? "", date: names ?? "", subject: names ?? "", status: names ?? ""))
}
And My JSON Response Is:
"attendence": [
{
"attendance_time": "15:35:34.787322",
"student__last_name": "acharya",
"student__first_name": "mahendra",
"subject__name": "Physiology",
"attendance_date": "2019-07-15",
"status": 1
}
How to append those values to Model Class, Any help will be appreciated.
Upvotes: 0
Views: 83
Reputation: 48
This way will work with no doubt :
Alamofire.request(displayURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseString { (response) in
if let mresponse = response.value {
var jsondata : [String : Any] = [:]
if let data = mresponse.data(using: String.Encoding.utf8) {
do {
//json data extracted as a dictionary
jsondata = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any]
} catch {
print(error.localizedDescription)
}
}
if let da = jsondata["attendence"] as? [[String : Any]] {
for item in da {
print(item["student__first_name"] ?? "NO")
var names = item["student__first_name"] as! String
self.arrData.append(DisplayAttendanceModel(name: names ?? "", date: names ?? "", subject: names ?? "", status: names ?? ""))
}
}
}
}
Upvotes: 1
Reputation: 3514
You should create model like this:
Models
class Attendence: Codable {
var attendence: [DisplayAttendanceModel]?
}
class DisplayAttendanceModel: Codable {
let firstName: String?
let lastName: String?
let date: String?
let time: String?
let subject: String?
let status: Int?
enum CodingKeys: String, CodingKey {
case firstName = "student__first_name"
case lastName = "student__last_name"
case date = "attendance_date"
case time = "attendance_time"
case subject = "subject__name"
case status
}
}
And then you can use like this:
Request
Alamofire.request(displayURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
if response.result.value != nil {
guard let data = response.data { return }
do {
let myData = try JSONDecoder().decode(Attendence.self, from: data)
print("myData is: \(myData)")
} catch let error {
print(error)
// Do something in error case
}
I hope it is work.
Enjoy.
Upvotes: 3
Reputation: 100503
You can try
Alamofire.request(displayURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers).responseData { (response) in
guard let data = response.data else { return }
do {
let res = try JSONDecoder().decode(Root.self, from:data)
print(res)
}
catch {
print(error)
}
}
// MARK: - Empty
struct Root: Codable {
let attendence: [Attendence]
}
// MARK: - Attendence
struct Attendence: Codable {
let name, date, subject:String
let status: Int
enum CodingKeys: String, CodingKey {
case name = "student__first_name"
case subject = "subject__name"
case date = "attendance_date"
case status
}
}
Upvotes: 2