问题
I have displayed first json data in tableview but when trying to display array data of inner array data it's showing blank data on table view , I have tired many ways sometimes it showing me Index out of range don't know where i'am getting wrong or forget to write code , I'am able to display that Fees class data in table view but not able to display description data ,Or do i need to change my Ui desgin
here i have model 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
}
}
code for getting JSON data
@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)
}
}
}
for display to 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
}
}
and JSON api data format
[{
"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
}]
I am confused to display that description in tableview , how it can be displayed data of description datas ??
回答1:
Basically there is nothing wrong in your code at all, But what you think and what data actually has is the main conflict, let me tell you where is the exact problem making you crazy.
Clearly observe Your json response. I have edited first record in your data and does not changed anything to the second record. Observe your Description value in 2 record.
[{
"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
}]
What i have done in your first record is.
This is the actual data:
"Description": "[{\"des\":\"Admission\",\"Amount\":1200},{\"des\":\"Due\",\"Amount\":0}]"
Resultant data after i removed quotation marks and backward slashes.
"Description": [{"des":"Admission","Amount":1200},{"des":"Due","Amount":0}]
That's it if you run your code for this type of data then you get what you want. What you are mistake is you are accessing data from a string value, what i mean is
"Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",
in the above data the value of your description key is a string type, but you are assuming it as an array of dictionaries and accessing those records that is the mistake. To solve this just make sure your data should be like as in below format.
[{
"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
}]
来源:https://stackoverflow.com/questions/45136583/display-nested-array-from-json-data-to-tableview-using-alamofire