我使用LinkedInSwift在用戶數據通過身份驗證後獲取其數據。我已經能夠打印出顯示其數據的我的response
。但我現在試圖將這些數據解析爲數據模型。使用Swift(LinkedIn API)將數據模型中的JSON響應解析爲
我有一個User
類:
typealias JSON = [String: Any]
class User {
var id: String?
var firstName: String?
var lastName: String?
init(json: JSON) {
guard let id = json["id"] as? String, let firstName = json["firstName"] as? String, let lastName = json["lastName"] as? String else { return }
self.id = id
self.firstName = firstName
self.lastName = lastName
}
,這裏是可以得到方法LinkedIn:
linkedinHelper.authorizeSuccess({ (lsToken) -> Void in
//Login success lsToken
print("User has logged in succesfully!")
//Check if the user user is logged in and perform and action if they are.
if self.linkedinHelper.lsAccessToken != nil {
self.linkedinHelper.requestURL("https://api.linkedin.com/v1/people/~:(id,first-name,last-name)?format=json",
requestType: LinkedinSwiftRequestGet,
success: { (response) -> Void in
print(response)
//Request success response
}) { [unowned self] (error) -> Void in
print(error.localizedDescription)
//Encounter error
}
} else {
}
}, error: { (error) -> Void in
print("Uh oh, there was an issue.")
//Encounter error: error.localizedDescription
}, cancel: {() -> Void in
print("Cancelled")
//User Cancelled!
})
}
我都對着這個不同的地方,但它似乎只是例子和文檔停在response
或需要第三方框架來解析數據。有人可以幫助指導我實現我的目標嗎?
更新打印response
的
結果:
<LSResponse - data: {
firstName = Joe;
id = htcxTEeLk4;
lastName = Smith;
},
你可以添加你所提供的JSON數據? –
@ZonilyJame請參閱最新的問題。 – Chace
你應該使用'response.jsonObject'而不是'response' –