我必须使用EasyMapping。当我解析Json的字典时,它的效果很好。但是现在我只需要解析对象数组,所以没有任何关键路径。我有杰森:
[
{
"key1": "dee",
"key2": 3232
},
{
"key1": "deeweewregre",
"key2": 5555
}
]
因此,我创建了2个自定义类。第一个用于数组中的元素:class TheEntry: EKObjectModel {
var key1: String!
var key2: Int!
}
extension TheEntry {
override class func objectMapping() -> EKObjectMapping{
let mapping = EKObjectMapping(objectClass: self)
mapping.mapPropertiesFromArray(["key1", "key2"])
return mapping
}
}
另一个用于数组本身:class TheList: EKObjectModel {
var entries: [TheEntry]!
}
extension TheList {
override class func objectMapping() -> EKObjectMapping{
let mapping = EKObjectMapping(objectClass: self)
mapping.hasMany(TheEntry.self, forKeyPath: mapping.rootPath)
return mapping
}
}
但这是行不通的。我的应用程序在mapping.rootPath上崩溃。但是我不知道还有其他方法可以使用Json的根级别。 最佳答案
这样尝试
mapping.hasMany(TheEntry.self, forKeyPath: "@self")
关于ios - 使用EasyMapping(Swift)解析Json数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36682400/