Wor*_*ing 5 arrays json uitableview ios swift2
我需要构建Arrays一个Grouped UITableView,每个表格单元格中都有一个Title和Detail行.我从服务器获得了json输出,使其形状正确,以迭代UITableViewDataSource方法.但是,将这些UITableView函数引用的可读数组转换为最简单的方法是什么?
标题数组用于组标题,因此它只是一维数组.我可以迭代一下.标题和细节数组都是二维的.我无法弄清楚如何在Swift中做到这一点.
"headings":["Tuesday, August 16, 2016","Wednesday, August 17, 2016","Thursday, August 18, 2016","Friday, August 19, 2016","Saturday, August 20, 2016","Sunday, August 21, 2016","Monday, August 22, 2016","Tuesday, August 23, 2016","Wednesday, August 24, 2016","Thursday, August 25, 2016","Friday, August 26, 2016","Saturday, August 27, 2016","Sunday, August 28, 2016","Monday, August 29, 2016","Tuesday, August 30, 2016","Wednesday, August 31, 2016","Thursday, September 1, 2016","Friday, September 2, 2016","Saturday, September 3, 2016","Sunday, September 4, 2016","Monday, September 5, 2016","Tuesday, September 6, 2016","Wednesday, September 7, 2016","Thursday, September 8, 2016","Friday, September 9, 2016","Saturday, September 10, 2016","Sunday, September 11, 2016","Monday, September 12, 2016","Tuesday, September 13, 2016","Wednesday, September 14, 2016","Thursday, September 15, 2016","Friday, September 16, 2016"],
"titles":[["Joe Johnson"],["Joe Johnson"],["Sandy Primmell","Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Sandy Primmell","Joe Johnson"],["Joe Johnson","Joe Johnson"],["Sandy Primmell","Joe Johnson"],["Mark Greene","Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Sandy Primmell","Joe Johnson"],["Joe Johnson"],["Sandy Primmell","Joe Johnson"],["Mark Greene","Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"],["Joe Johnson"]],
"details":[["OFF"],["OFF"],["Gregory","OFF"],["Gregory"],["OFF"],["OFF"],["OFF"],["Weekday Rounders","OFF"],["Weekday Rounders","Night Owls"],["Gregory","OFF"],["Gregory","OFF"],["OFF"],["OFF"],["OFF"],["Gregory"],["Gregory","OFF"],["Gregory"],["Gregory","OFF"],["Gregory","OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"],["OFF"]]
Run Code Online (Sandbox Code Playgroud)
UPDATE
这是抓取数据的Alamofire异步函数:
manager.request(.POST, getRouter(), parameters:["dev": 1, "app_action": "schedule", "type":getScheduleType(), "days_off":getScheduleDaysOff(), "period":getSchedulePeriod(), "begin_date":getScheduleBeginDate(), "end_date":getScheduleEndDate()])
.responseString {response in
print(response)
var json = JSON(response.result.value!);
// what I'm missing
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用Argo或SwiftyJSON等 JSON 解析库,它们是为了简化 JSON 解析而创建的。它们都经过了良好的测试,并且会为您处理边缘情况,例如 JSON 响应中缺少参数等。
假设 JSON 响应具有这种格式(来自Twitter API)
{
"users": [
{
"id": 2960784075,
"id_str": "2960784075",
...
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这Response是一个包含数组的类,User该数组是此处未显示的另一个类,但您明白了。
struct Response: Decodable {
let users: [User]
let next_cursor_str: String
static func decode(j: JSON) -> Decoded<Response> {
return curry(Response.init)
<^> j <|| "users"
<*> j <| "next_cursor_str"
}
}
Run Code Online (Sandbox Code Playgroud)
//Convert json String to foundation object
let json: AnyObject? = try? NSJSONSerialization.JSONObjectWithData(data, options: [])
//Check for nil
if let j: AnyObject = json {
//Map the foundation object to Response object
let response: Response? = decode(j)
}
Run Code Online (Sandbox Code Playgroud)
正如官方文档中所解释的:
if let dataFromString = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
let json = JSON(data: dataFromString)
}
Run Code Online (Sandbox Code Playgroud)
如果数据是数组,则使用索引
//Getting a double from a JSON Array
let name = json[0].double
Run Code Online (Sandbox Code Playgroud)
如果数据是字典,则使用键
//Getting a string from a JSON Dictionary
let name = json["name"].stringValue
Run Code Online (Sandbox Code Playgroud)
大批
//If json is .Array
//The `index` is 0..<json.count's string value
for (index,subJson):(String, JSON) in json {
//Do something you want
}
Run Code Online (Sandbox Code Playgroud)
字典
//If json is .Dictionary
for (key,subJson):(String, JSON) in json {
//Do something you want
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3091 次 |
| 最近记录: |