[英]Handling JSON with Alamofire & SwiftyJSON and Adding to UITableView in Swift
I want to use Alamofire and SwiftyJSON for my REST API. 我想将Alamofire和SwiftyJSON用于我的REST API。 I have accessed the root JSON but I can't access the objects of JSON.
我已经访问了JSON根目录,但是无法访问JSON对象。
This my json result: 这是我的json结果:
[
{
"ID": 1,
"name": "JABODETABEK",
"cabang": [
{
"ID": 1,
"wilayah_id": 1,
"name": "Jembatan Lima"
},
{
"ID": 2,
"wilayah_id": 1,
"name": "Kebon Jeruk"
}
]
},
{
"ID": 2,
"name": "Sumatra Selatan dan Bangka Belitung",
"cabang": [
{
"ID": 6,
"wilayah_id": 2,
"name": "A. Yani - Palembang"
},
{
"ID": 7,
"wilayah_id": 2,
"name": "Veteran - Palembang"
}
]
}
}
With this code: 使用此代码:
Alamofire.request(url).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar.arrayObject {
self.productArray = resData as! [[String:AnyObject]]
print("MyArray: \(self.productArray)")
}
}
}
My tableview: 我的表视图:
func numberOfSections(in tableView: UITableView) -> Int {
return self.productArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let dic = productArray[section]
return dic["name"] as? String
}
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
return (((?)))
}
Please help me in viewing the datas in my tableView cells using Alamofire and SwiftyJSON. 请帮助我使用Alamofire和SwiftyJSON在tableView单元中查看数据。 How can I access that data?
如何访问该数据? I mean how can I get
numberOfRowsInSection
? 我的意思是我如何获得
numberOfRowsInSection
?
Try this: 尝试这个:
Alamofire.request("url").responseJSON { (responseData) -> Void in
if let data = response.data {
guard let json = try? JSON(data: data) else { return }
self.productArray = json.arrayValue //productArray type must be [[String:AnyObject]]
}
after that update tableView delegate functions: 之后,更新tableView委托函数:
func numberOfSections(in tableView: UITableView) -> Int {
return self.productArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let dic = productArray[section]
return dic["name"].string
}
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
return productArray[sectionInd]["cabang"].arrayValue.count
}
First thing is don't use Dictionary or array of Dictionary now. 第一件事是现在不要使用Dictionary或Dictionary数组。 You can use Codable instead https://medium.com/@multidots/essentials-of-codable-protocol-in-swift-4-c795a645c3e1 ,
您可以使用Codable代替https://medium.com/@multidots/essentials-of-codable-protocol-in-swift-4-c795a645c3e1 ,
if you are using dictionary then use Any instead of AnyObject. 如果使用字典,则使用Any代替AnyObject。
Your answer is (self.productArray[sectionInd]["cabang"] as! [[String:Any]]).count
您的答案是
(self.productArray[sectionInd]["cabang"] as! [[String:Any]]).count
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
return ((self.productArray[sectionInd]["cabang"] as! [[String:Any]]).count
}
//MARK:- UITableView Delegate & DataSource // MARK:-UITableView委托和数据源
func numberOfSections(in tableView: UITableView) -> Int {
return self.productArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let dictionaryProduct = self.productArray.object(at: section) as! NSDictionary
let arrayCabang = dictionaryProduct.object(forKey: "cabang") as! NSArray
if arrayCabang.count > 0 {
return arrayCabang.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let dictionaryProduct = self.productArray.object(at: indexPath.section) as! NSDictionary
let arrayCabang = dictionaryProduct.object(forKey: "cabang") as! NSArray
if arrayCabang.count > 0 {
let dictionaryCurrentCabang = arrayCabang.object(at: indexPath.row) as! NSDictionary
//Here you get data of cabangs at the particular index
}
return cell!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.