ios - 使用 Swift (LinkedIn API) 将 JSON 响应解析为数据模型

标签 ios json swift linkedin-api

我正在使用 LinkedInSwift 在用户验证其凭据后获取用户数据。我已经能够打印出显示其数据的 response。但我现在正在尝试将该数据解析为数据模型。

我有一个 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;
},

最佳答案

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
}

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

                                        let user = User(json: response.jsonObject)


                                        //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!
    })
}

关于ios - 使用 Swift (LinkedIn API) 将 JSON 响应解析为数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46760124/

相关文章:

swift - 需要澄清 Swift 中的 AnyObject

ios - IBDesignable 在框架中不适用于嵌入式项目

ios - 通过按按钮插入 TableView 行

ios - 如何在 Swift 中发出 HTTP 请求 + 基本身份验证

ios - RxSwift 绑定(bind)到变量的格式化值

java - 避免使用代表 JSON 的 POJO 的样板代码

javascript - 如何从具有值的 JSON 对象中获取索引?

ios - UIAlertView 的类别需要回调警报按钮单击 iOS

ios - 以 objc 模式建模许多不同的类

javascript - 如何从 AJAX 读取此 JSON 响应