gpt4 book ai didi

ios - ObjectMapper 因根元素而失败

转载 作者:行者123 更新时间:2023-11-30 13:06:34 25 4
gpt4 key购买 nike

我遇到了一个我不知道如何解决的问题。

当我尝试将此 JSON 映射到 User 对象时,它失败了:

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"
}
}

我想映射到我的用户类别:

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 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"]
}
}

关于ios - ObjectMapper 因根元素而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39290404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com