1

I'm still new at JSON and Alamofire. I have spent a day to understand and applied it to my project basically. The API structure is look like as below:

{ "data": [ { "id": 1, "amount": 20000, "amount_display": "200.00", "balance_amount": 20000, "account_balance_display": "200.00" }, { "id": 2, "amount": 10000, "amount_display": "100.00", "balance_amount": 29000, "account_balance_display": "290.00" } ] } 

I want to display "id", "amount_display" and "account_balance_display" in tableview cell. But I really don't have any idea, even to read the json.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"TableViewCell", for: indexPath) as! TableViewCell var dict = array[indexPath.row] cell.lbl1.text = dict["id"] as? String cell.lbl2.text = dict["amount_display"] as? String cell.lbl3.text = dict["account_balance_display"] as? String return cell } 

and :

static let kdata = "data" var responseArray: NSArray = [] override func viewDidLoad() { super.viewDidLoad() Alamofire.request("myURL").responseJSON { response in //print (response) if let balanceJSON = response.result.value { let dataObject:Dictionary = balanceJSON as! Dictionary<String, Any> // print(dataObject) let balanceObject = dataObject[TopupTableViewController.kdata] as! NSArray print (balanceObject) } } } 

this what I have already done

3
  • stackoverflow.com/questions/40554279/… Commented Apr 7, 2018 at 9:25
  • @UmaMadhavi tq... :) Commented Apr 7, 2018 at 9:30
  • What you have done is just right. Make array used in your tableView's data source equal to balanceObject is the simplest way. Commented Apr 7, 2018 at 9:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.