如何根据子节点中的值获取值。例如 如果我有这样的JSON
{
"id": 14,
"name": "АBC",
"picture": 44,
"group": {
"id": 2,
"gname": "AAA",
"pic": 1
}
},
{
"id": 14,
"name": "АBC",
"picture": 44,
"group": {
"id": 1,
"gname": "BB",
"pic": 2
}
}...
]
我想获取子值为“ AAA”的所有名称的列表
我知道如何读取名称的所有值,但是如果我只想要具有特定gname值的名称,我不知道如何读取它们。
这是代码:
import UIKit
import Alamofire
import SwiftyJSON
class ThirdViewController : UITableViewController {
var dataInfo = [[String: AnyObject]]()
let url = "http://XXX"
let header = ["X": "XXX"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 50
getData(url: url)
self.tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataInfo.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
cell.lblName?.text = dataInfo[indexPath.row]["name"] as? String
return cell
}
func getData(url: String) {
Alamofire.request(url, method: .get, headers: header)
.responseJSON { response in
if response.result.isSuccess {
print("Sucess! Got the data")
let dataJSON : JSON = JSON(response.result.value!)
self.updateData(json: dataJSON)
} else {
print("Error: \(String(describing: response.result.error))")
}
}
self.tableView.reloadData()
}
func updateData(json : JSON) {
if let data = json[].arrayObject {
self.dataInfo = data as! [[String: AnyObject]]
self.tableView.reloadData()
}
else {
print("cannot connect to the server")
}
}
}
如果有人可以帮助我,我将不胜感激。
答案 0 :(得分:0)
首先,您应该使用
Codable
而不是SwiftyJSON
所以您必须添加这些模型
struct Group: Codable {
var id: Int?
var gname: String?
var pic: Int?
enum CodingKeys: String, CodingKey {
case id, gname, pic
}
}
struct User: Codable {
var id: Int?
var name: String?
var picture: Int?
var group: Group?
enum CodingKeys: String, CodingKey {
case id, name, picture, group
}
}
现在您必须将其添加到Alamofire.request
let jsonDecoder = JSONDecoder()
let dataJSON = try? jsonDecoder.decode([User].self, from: response.data!)
现在是过滤器Thing,如果您只需要带有“ AAA”的元素,我们可以像这样过滤阵列
let users: [User] = []
let searchString = "AAA"
let filterResults = users.filter({
$0.group.filter({$0.gname == searchString}).count > 1
})
所以整个代码看起来像
class ThirdViewController : UITableViewController {
var dataInfo: [User] = []
let url = "http://XXX"
let header = ["X": "XXX"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 50
getData(url: url)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataInfo.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! Cell2
cell.lblName?.text = dataInfo[indexPath.row].name
return cell
}
func getData(url: String) {
Alamofire.request(url, method: .get, headers: header)
.responseJSON { response in
if response.result.isSuccess {
print("Sucess! Got the data")
guard let responseData = response.data else {
print("didn't get any data from API")
return
}
let jsonDecoder = JSONDecoder()
let dataJSON = try? jsonDecoder.decode([User].self, from: responseData)
// Search data
let searchString = "AAA"
let filterResults = users.filter({
$0.group.gname == searchString
})
self.dataInfo = filterResults
// Reload Data
self.tableView.reloadData()
} else {
print("Error: \(String(describing: response.result.error))")
}
}
}
}