Swift字典检索值

时间:2017-02-26 01:15:28

标签: swift dictionary

您好我使用SDK中的链接登录我的应用程序。

API返回一个json字符串,我已将其转换为字典对象。

使用下面的代码我可以成功检索到一些值。

  

让firstName:NSString? = dict?[" firstName"]为?的NSString

如何检索公司名称和职称?

["publicProfileUrl": https://www.linkedin.com/in/joebloggs, "formattedName": Joe Bloggs, "id": Zazobgtf1Q, "pictureUrls": {
    "_total" = 1;
    values =     (
        "https://media.licdn.com/mpr/mprx/0_xBXVf6v56zJf42DuUQJy70N69gfspmi8VYJMYZq_Q6X8SCm_a-4jUmz6FF4wOai_xjJMpPN_qiNQ7xaiUpvv4jq5_iN67xx8apv4S6HL9JLf7HadaZ5JIuNzlJ"
    );
}, "pictureUrl": https://media.licdn.com/mpr/mprx/0_OzOPtLSS9GH8gFDYUA6-xtfSKPkgY5YmNkEPBAGSc5ypRvS_AzXjVcuSNssssXO_qkE1Mi_DxXcyZT2mBQR7sAa3VXcjZTdGlQRKlrR2ALGltB-YjcSOAkzjslDDATY14Lxx9mjGA2G, "lastName": Bloggs, "emailAddress": joe@gmail.com, "positions": {
    "_total" = 1;
    values =     (
                {
            company =             {
                id = 9433004;
                industry = "Marketing & Advertising";
                name = "Company Name";
                size = "2-10";
                type = "Privately Held";
            };
            id = 865817330;
            isCurrent = 1;
            location =             {
                country =                 {
                    code = gb;
                    name = "United Kingdom";
                };
                name = "Leeds, United Kingdom";
            };
            startDate =             {
                month = 9;
                year = 2016;
            };
            title = "Staff";
        }
    );
}, "firstName": Joe]

以下是我用于linkedIn登录的代码:

 func linkedInLogin(){

        LISDKSessionManager.createSession(withAuth: [LISDK_BASIC_PROFILE_PERMISSION, LISDK_EMAILADDRESS_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: { (returnState) -> Void in
            print("success called!")
             let session = LISDKSessionManager.sharedInstance().session
            //let url = "https://api.linkedin.com/v1/people/~"

            let url = "https://api.linkedin.com/v1/people/~:(id,summary,positions,email-address,first-name,last-name,public-profile-url,formatted-name,picture-url,picture-urls::(original))?format=json"

            if LISDKSessionManager.hasValidSession() {
                LISDKAPIHelper.sharedInstance().getRequest(url, success: { (response) -> Void in
                   // print(response!.data!)


                    let str = response!.data!

                    let dict = self.convertToDictionary(text: str)




                    print(dict!)

                    let firstName : NSString? = dict?["firstName"] as? NSString
                    let lastName : NSString? = dict?["lastName"] as? NSString
                    let email : NSString? = dict?["emailAddress"] as? NSString
                    let userName : NSString? = dict?["formattedName"] as? NSString
                    let linkedInID : NSString? = dict?["id"] as? NSString
                    let link : NSString? = dict?["publicProfileUrl"] as? NSString
                    let liid : NSString? = dict?["id"] as? NSString
                    let picurl : NSString? = dict?["pictureUrl"] as? NSString
                   // let summary : NSString? = dict?["summary"] as? NSString
                    let positions : NSString? = dict?["positions"] as? NSString




                }, error: { (error) -> Void in
                    print(error!)
                })
            }

        }) { (error) -> Void in
            print("Error: \(error)")
        }

     }


    func convertToDictionary(text: String) -> [String: Any]? {
        if let data = text.data(using: .utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
            } catch {
                print(error.localizedDescription)
            }
        }
        return nil
    }

0 个答案:

没有答案