ObjectMapper 因根元素而失败
ObjectMapper fails with root element
我遇到了一个我不知道如何解决的问题。
当我尝试将此 JSON 映射到用户对象时,它失败了:
SwiftyJSON 在“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"
}
}
我想映射到我的用户 class:
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 {}
}
由于您没有提供接收响应的代码,我使用测试用例进行了测试,并将响应放入 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 更改为:
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"]
}
}
我遇到了一个我不知道如何解决的问题。
当我尝试将此 JSON 映射到用户对象时,它失败了:
SwiftyJSON 在“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"
}
}
我想映射到我的用户 class:
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 {}
}
由于您没有提供接收响应的代码,我使用测试用例进行了测试,并将响应放入 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 更改为:
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"]
}
}