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

时间:2017-10-15 21:21:42

标签: 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;
},

1 个答案:

答案 0 :(得分:1)

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