我正面临一个我不确切知道如何解决的问题。
当我尝试将此JSON映射到User对象时,它失败了:
SwiftyJSON at" 2.3.3"
JSON:
{
"results":[
{
"gender":"male",
"name":{
"title":"mr",
"first":"donald",
"last":"boor"
},
"location":{
"street":"1781 vredenburg",
"city":"het bildt",
"state":"noord-brabant",
"postcode":30431
},
"email":"donald.boor@example.com",
"login":{
"username":"greenpeacock269",
"password":"lambda",
"salt":"2uqekB9c",
"md5":"4fd97ba849fc6d1abb23134cd554dba8",
"sha1":"d5adf8eadc9c8b33422ccbc8087333e6dec6f83e",
"sha256":"2e4b3344d72dc6937fee422149146cb83b6397530e6afdb12daad165abe2b3d9"
},
"dob":"1986-12-25 21:32:21",
"registered":"2016-02-25 06:58:50",
"phone":"(365)-252-9092",
"cell":"(962)-625-3483",
"id":{
"name":"BSN",
"value":"59041461"
},
"picture":{
"large":"https://randomuser.me/api/portraits/men/15.jpg",
"medium":"https://randomuser.me/api/portraits/med/men/15.jpg",
"thumbnail":"https://randomuser.me/api/portraits/thumb/men/15.jpg"
},
"nat":"NL"
}
],
"info":{
"seed":"806e92dbf96026d0",
"results":1,
"page":1,
"version":"1.1"
}
}
我想映射到我的用户类:
import Foundation
import ObjectMapper
class User: NSObject, Mappable {
var gender: String?
var title: String?
var first: String?
var last: String?
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
gender <- map["gender"]
title <- map["name.title"]
first <- map["name.first"]
last <- map["name.last"]
}
}
let user = Mapper<User>().mapArray(jsonData) //Doesn't work getting nil
let userList :Array<User> = Mapper<User>().mapArray(jsonData)! //fatal error: unexpectedly found nil while unwrapping an Optional value
响应代码:
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error == nil) {
do {
//let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let jsonResult = JSON(data: data!)
let dict = jsonResult["results"].dictionaryValue
let userList :Array<User> = Mapper<User>().mapArray(dict)!
print(userList)
} catch {}
}
答案 0 :(得分:1)
由于您尚未提供接收响应的代码,因此我使用测试用例进行了测试,并将响应放入test.json
文件中。
if let path = NSBundle.mainBundle().pathForResource("test", ofType: "json") {
do {
let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
do {
let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
let userList :Array<TestUser> = Mapper<TestUser>().mapArray(jsonResult.objectForKey("results"))!
print(userList)
} catch {}
} catch {}
}
修改强>
我已经实现了SwiftyJSON
语法,请尝试以下代码行:
let userList :Array<TestUser> = Mapper<TestUser>().mapArray(jsonResult["results"].arrayObject)!
print(userList)
将模型类更改为:
class TestUser: NSObject, Mappable {
var gender: String?
var title: String?
var first: String?
var last: String?
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
gender <- map["gender"]
title <- map["name.title"]
first <- map["name.first"]
last <- map["name.last"]
}
}
答案 1 :(得分:-1)
设置变量类型AnyObject =&gt;尝试(和我一起工作)