【发布时间】:2017-02-15 02:22:28
【问题描述】:
我无法将我的 Dictionary json 放到我的“MyOwnClass”中。在 Swift 2 中一切都很好。
我的 Json 结构:
[
{
"dataString1" : "value",
"dataInt1" : 3277
},
{
"dataString1" : "value",
... and so on
我在 Swift 2.x 中使用了 Objectmapper,但现在我想在 Swift 3 中转换所有内容。
我的旧代码:
do {
let jsonSerilization = try NSJSONSerialization.JSONObjectWithData(json, options: NSJSONReadingOptions.MutableContainers) as! NSArray
for jsonCount in 0..<jsonSerilization.count {
let singleData = Mapper<MyOwnClass>().map(jsonSerilization[jsonCount])
print(singleData.dataString1) //gotValue
}
} catch {
...
}
现在jsonSerilization[jsonCount] 行是一个字典。在控制台中,我找到了我所有的值。
但我希望这本字典在我的“MyOwnClass”结构中。我无法将我的字典转换为字符串。我试过 dictionary.description 但这个字符串的格式有误。
MyOwnClass 结构并不特别。
class MyOwnClass : Mappable {
var dataString1: String?
var dataInt1: Int?
required init?(map: Map){
mapping(map: map)
}
func mapping(map: Map) {
dataString1 <- map["dataString1"]
dataInt1 <- map["dataInt1"]
}
}
如何在“MyOwnClass”中传输字典中的值? 当然我可以手动完成,但我想要一个自动的解决方案。
我搜索了很多将字典转换为 json 字符串,但没有任何帮助。谢谢
编辑 看来 ObjectMapper 无法解析这种 json 结构(状态:10/2016) 如果我错了,请告诉我。
【问题讨论】:
标签: ios swift dictionary swift3 objectmapper