【发布时间】:2019-12-27 18:38:24
【问题描述】:
我使用 Swift 4 编写,使用 viper 结构和 ObjectMapper 将我的 JSON 响应映射到我的模型。
我正在尝试使用动态键映射这个相当复杂的 JSON 响应,并希望得到一些关于我做错了什么的反馈。
整月上传的文档以月份名称为键返回,其所有文档列表为值。我的回复是这样的json:
{
"results": {
"2019-08": [
{
"id": 2,
"user_id": 7,
"document": "1566282328atlassian-git-cheatsheet1.pdf",
"name": "atoz",
"order": 0,
"is_edit": 0,
"edit_json": "",
"created_at": "2019-08-20 06:25:28",
"updated_at": "2019-08-20 06:25:28",
"date": "2019-08",
"url": "http://35.154.206.145/storage/pdf/1566282328atlassian-git-cheatsheet1.pdf"
},
{ ….}
],
"2019-07": [
{
"id": 2,
"user_id": 7,
"document": "1566282328atlassian-git-cheatsheet1.pdf",
"name": "atoz",
"order": 0,
"is_edit": 0,
"edit_json": "",
"created_at": "2019-08-20 06:25:28",
"updated_at": "2019-08-20 06:25:28",
"date": "2019-08",
"url": "http://35.154.206.145/storage/pdf/1566282328atlassian-git-cheatsheet1.pdf"
},
{ ….}
]
}
}
我的模型类是这样在mapper模型类中获取数据的
import ObjectMapper
struct GroupResponse: Mappable {
init?(map: Map) {}
var results: [String: DocumentObject]?
mutating func mapping(map: Map) {
results <- map["results"]
}
}
class DocumentObject: Mappable{
internal var months: [String: [DocumentListObject]]?
required init?(map: Map) {}
func mapping(map: Map) {
for (monthKey, monthValue) in map.JSON as! [String: [String: Any]] {
let month = DocumentListObject()
months?[monthKey] = [month]
}
}
}
class DocumentListObject {
var id:Int?
var user_id:Int?
var document:String?
var name:String?
var order:Int?
var is_edit:Bool?
var edit_json:String?
var date:String?
var url:String?
}
这有什么问题,我在 api 响应中获取它时得到 nil 并崩溃
if let json = data as AnyObject? {
let arrayResponse = json as! NSDictionary
let arrayObject = Mapper<GroupResponse>().mapDictionary(JSON: arrayResponse as! [String : [String : Any]]) // I got crash here
print(arrayObject)
【问题讨论】:
标签: ios json swift objectmapper