我已经在tableview中显示了第一个json数据,但是当试图显示内部数组数据的数组数据时,它在表视图中显示空白数据,我已经厌倦了许多方法,有时它会让我看到索引超出范围而且#39 ;我知道我在哪里出错或忘记编写代码,我能够在表视图中显示费用类数据但不能显示描述数据,或者我是否需要更改我的Ui设计< / p>
这里我有模特课
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中显示该描述,如何显示描述数据的数据??
答案 0 :(得分:1)
基本上你的代码根本就没有错,但你的想法和实际上有什么数据是主要的冲突,让我告诉你哪里是让你疯狂的确切问题。
清楚地观察你的json反应。我已经在您的数据中编辑了第一条记录,并且没有更改任何内容到第二条记录。在2记录中观察您的描述值。
[{
"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}]
如果您运行此类数据的代码,那么就可以得到您想要的内容。你错的是你从字符串值访问数据,我的意思是
"Description": "[{\"des\":\"Admission Fee\",\"Amount\":2000},{\"des\":\"Due\",\"Amount\":1200}]",
在上面的数据中,你的描述键的值是一个字符串类型,但你假设它是一个字典数组并访问那些错误的记录 。要解决此问题,请确保您的数据应符合以下格式。
[{
"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
}]