使用Alamofire将JSON数据的嵌套数组显示到tableview
我已经在tableview中显示了第一个json数据,但是当试图显示内部数组数据的数组数据时,它显示在表视图上的空白数据,我已经厌倦了很多方式有时它显示我索引超出范围不知道我在哪里出错或忘记编写代码,我可以在表视图中显示费用类数据,但不能显示描述数据,或者我是否需要更改我的Uidevise
这里我有模特class
class Fees { var Billno = String() var DateOfReciept = String() var amount = String() var status = String() var recivedDate = String() var AmountPaid = String() var descriptions = [Description]() init(feesJson:JSON) { self.Billno = feesJson["RecieptNo"].stringValue self.DateOfReciept = feesJson["DateOfReciept"].stringValue self.amount = feesJson["dueAmount"].stringValue self.status = feesJson["Status"].stringValue self.recivedDate = feesJson["recivedDate"].stringValue self.AmountPaid = feesJson["AmountPaid"].stringValue if let description = feesJson["Description"] as? JSON, let desArray = description.array{ for desc in desArray{ let desfees = Description(feedesJson: desc) self.descriptions.append(desfees) } } } } class Description{ var amountdes = String() var des = String() init(feedesJson:JSON){ self.amountdes = feedesJson["Amount"].stringValue self.des = feedesJson["des"].stringValue } }
获取JSON数据的代码
@IBOutlet weak var tableview1: UITableView! var fees : [Fees] = [] var descriptionFe : [Description] = [] func getFees() { let defaults = UserDefaults.standard let student_id = defaults.string(forKey: "masteridKey") let std_id_String = student_id?.replacingOccurrences(of: "[^0-9 ]", with: "", options: NSString.CompareOptions.regularExpression, range:nil) print("numbericpending",std_id_String!) let url = NSURL(string: "http://192.168.100.5:84/api/financeApi/getAllFees" + "?ID=" + std_id_String! + "&fromDate=" + "&toDate=") var request = URLRequest(url: url! as URL) request.httpMethod = "GET" request.setValue("application/json", forHTTPHeaderField: "Content-Type") Alamofire.request(request).responseJSON(){ response in switch response.result{ case.success(let data): print("success",data) let myresponse = JSON(data) print("tabledata",myresponse) // dictdata = myresponse.arrayObject for fee in myresponse.array! { let feesObj = Fees(feesJson: fee) self.fees.append(feesObj) // here I'am getting array data but not descripition datas and while calling from here to tableview its giving me blank } for fee in myresponse.array! { //self.descriptionFe = feesObj.descriptions let feesdesc = Description(feedesJson: fee) self.descriptionFe.append(feesdesc) } self.tableview1.reloadData() case.failure(let error): print("Not Success",error) } } }
用于显示到tableview
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == tableview1{ return fees.count }else{ return descriptionFe.count } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == tableview1{ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell let getdata = fees[indexPath.row] cell.billno_txt.text = getdata.Billno cell.received_date_txt.text = getdata.recivedDate cell.status_txt.text = getdata.status cell.total_amount_txt.text = getdata.AmountPaid cell.date_txt.text = getdata.recivedDate return cell } else{ let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell // // let getdatafees = fees[indexPath.row] let getdatadesciption = self.descriptionFe[indexPath.row] cell.inner_txt1.text = getdatadesciption.des // cell.inner_txt2.text = fees[indexPath.row].AmountPaid return cell } }
和JSON API数据格式
[{ "StdID": 95, "Status": "D", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-06-08T00:00:00", "MasterID": "E0017", "RecieptNo": 83, "DateOfReciept": "2017-06-08T00:00:00", "Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]", "AmountPaid": 1200, "dueDate": "2017-06-29T00:00:00", "dueAmount": 1200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }, { "StdID": 95, "Status": "P", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-07-13T00:00:00", "MasterID": "E0017", "RecieptNo": 1171, "DateOfReciept": "2017-07-01T00:00:00", "Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]", "AmountPaid": 3200, "dueDate": "2017-07-30T00:00:00", "dueAmount": 3200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }]
我很困惑,在tableview中显示该描述,它如何显示描述数据的数据?
基本上你的代码根本没有错,但是你的想法和数据实际上有什么是主要的冲突,让我告诉你哪个是让你疯狂的确切问题。
清楚地观察你的JSON响应。 我已经在你的数据中编辑了第一条logging,并没有改变任何东西到第二条logging。 观察2条logging中的描述值。
[{ "StdID": 95, "Status": "D", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-06-08T00:00:00", "MasterID": "E0017", "RecieptNo": 83, "DateOfReciept": "2017-06-08T00:00:00", "Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}], "AmountPaid": 1200, "dueDate": "2017-06-29T00:00:00", "dueAmount": 1200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }, { "StdID": 95, "Status": "P", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-07-13T00:00:00", "MasterID": "E0017", "RecieptNo": 1171, "DateOfReciept": "2017-07-01T00:00:00", "Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]", "AmountPaid": 3200, "dueDate": "2017-07-30T00:00:00", "dueAmount": 3200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }]
你在第一张唱片中所做的是
这是实际的数据:
"Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]"
我删除了引号和反斜线后生成的数据。
"Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}]
这就是如果你运行你的代码这种types的数据,那么你得到你想要的。 你错误的是你正在访问string值的数据,我的意思是
"Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",
在上面的数据中,你的描述键的值是一个stringtypes,但是你假定它是一个字典数组并且访问这些logging是错误的 。 要解决这个问题,只要确保你的数据应该像下面的格式。
[{ "StdID": 95, "Status": "D", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-06-08T00:00:00", "MasterID": "E0017", "RecieptNo": 83, "DateOfReciept": "2017-06-08T00:00:00", "Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}], "AmountPaid": 1200, "dueDate": "2017-06-29T00:00:00", "dueAmount": 1200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }, { "StdID": 95, "Status": "P", "NAME": "Calvin Patterson", "CLASSNO": "1", "recivedDate": "2017-07-13T00:00:00", "MasterID": "E0017", "RecieptNo": 1171, "DateOfReciept": "2017-07-01T00:00:00", "Description": [{"des":"Admission Fee","Amount":2000},{"des":"Due","Amount":1200}], "AmountPaid": 3200, "dueDate": "2017-07-30T00:00:00", "dueAmount": 3200, "reciever": "Mr. Adminstrator", "CLASS_ID": 2021, "receivedAmount": 0 }]