{
"3": {
"title": "something"
},
"28": {
"title": "something else"
}
}
如何使用ObjectMapper将此json转换为Content对象数组?
struct Content: Mappable {
var id: String? //Expected value is 3, 28
var title: String? //Expected value is "something", "something else"
}
提前致谢。
答案 0 :(得分:1)
我认为你正在寻找这样的事情:
JSON - 型号:
let content = Mapper<Content>().map(JSONString)
模型 - JSON:
let json = Mapper().toJSONString(content, prettyPrint: true)
在您使用它之前,您需要将Content Model
更改为以下内容:
struct Content: Mappable {
var id: String? //Expected value is 3, 28
var title: String? //Expected value is "something", "something else"
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
}
}
答案 1 :(得分:0)
在Swift 3中: 首先从这个词典中获取所有键,如此
keysArray = Array(dict.keys)
现在循环通过这个键Array&amp;你会得到这本词典中的所有键,如
for i in 0..<keysArray.count {
if let key = keysArray[i] as? String {
//get your key here
if let singleObject = dict[key] as? [String: Any] {
if let value = singleObject["title"] as? String {
//get you value
// get both key & value. Now map it. Hope you get it.
}
}
}
}
希望它有所帮助。