2017-10-15 41 views
0

我使用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; 
}, 
+1

你可以添加你所提供的JSON數據? –

+0

@ZonilyJame請參閱最新的問題。 – Chace

+0

你應該使用'response.jsonObject'而不是'response' –

回答

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

它給我一個錯誤,說:''(鍵:AnyHashable,值:任何)「預期參數類型'JSON'(又名'字典')',當我把它像這樣的JSON :'let user = LinkedInData(json:response.jsonObject as!JSON)'並打印'user?.firstName'我得到'Optional(「Joe」)'it。 – Chace

+0

firstName是可選的,將其解包並且可選將消失。 – hemant3370

+0

我試圖像這樣解開它'self.textLbl.text =「Hello/\(String(describe:user?.firstName!))」'但它不起作用:/ – Chace