使用Alamofire将嵌套数组从JSON数据显示到tableview [英] Display Nested array from JSON data to tableview using Alamofire

查看:99
本文介绍了使用Alamofire将嵌套数组从JSON数据显示到tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableview中显示了第一个json数据,但是当试图显示内部数组数据的数组数据时,它在表视图上显示空白数据,我已经厌倦了许多方式,有时它显示我的索引超出范围不知道我在哪里我错了或忘记编写代码,我能够在表视图中显示费用类数据但不能显示描述数据,或者我是否需要更改我的Ui设计

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

这里我有模型类

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数据的代码

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)
            }

        }


    }

用于显示到tableview

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
        }
    }

和JSON api数据格式

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
    }]

我很困惑在tableview中显示该描述,如何显示数据描述数据??

I am confused to display that description in tableview , how it can be displayed data of description datas ??

推荐答案

基本上你的代码没有任何问题,但你的想法和实际拥有的数据是什么是主要的冲突,让我告诉你哪里是让你疯狂的确切问题。

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.

清楚地观察你的json响应。我已经编辑了你的数据中的第一条记录而没有将任何内容更改为第二条记录。在2条记录中观察您的描述值。

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.

这是实际数据:

 "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
}]

这篇关于使用Alamofire将嵌套数组从JSON数据显示到tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆