【发布时间】:2018-08-10 03:05:29
【问题描述】:
鉴于以下示例 JSON
{
"filters": [
{ "name" : "First Type",
"types" : ["md", "b", "pb"]},
{ "name" : "Second Type",
"types" : ["pt", "ft", "t"]},
{ "name" : "Third Type",
"types" : ["c", "r", "s", "f"]
}
],
"jobs": [
{ "title":"f",
"description" : "descrip text",
"criteria":[ "md", "ft", "s" ],
"img" : "www1"
},
{ "title":"boa",
"description" : "a description",
"criteria":[ "b", "pb", "f", "ft" ],
"img" : "www2"
},
{ "title":"BK",
"description" : "something here",
"criteria":[ "md", "pt", "ft", "b", "s" ],
"img" : "www3"
}
]
}
(使用 Alamofire 创建响应) 让 responseJSON : JSON = JSON(response.result.value!)
1) 我正在尝试将它们转换为两个字符串数组。一个数组: let filter = [String : [String]] 和另一个数组用于作业。我该怎么做? (又名给男人一条鱼)以下是一些示例代码 sn-ps,但没有一个甚至接近工作。
let filterCategories = responseJSON["filters"].arrayValue.map({
$0["name"].stringValue
})
和
for (key,subJson):(String, JSON) in responseJSON["filters"] {
let object : filterObject = filterObject(category: key, list: subJson.arrayValue.map({ $0.stringValue }))
}
2) 我如何学习如何正确使用它? (又名教人钓鱼)我一直在阅读文档(https://github.com/SwiftyJSON/SwiftyJSON),但我很难理解它。我猜最终答案将使用 .map、.stringValue 和 .arrayValue。最终,尽管我试图避免大量不必要或无法管理的代码。
【问题讨论】:
-
如果你使用的是 Swift 4,那么看看Ultimate Guide to JSON Parsing with Swift 4
标签: ios arrays swift swifty-json