-3

I am a beginner with Swift. I am try to make a http request in Swift.

I am successful in doing it but I am not able to parse the response. I have searched a lot on this but could not find any thing.

This is my piece of code .

Output of the above code is:

{

  Login =     {

        AccountType = Pro;
        FileSizeLimit = 157286400;
        PasswordHash = fc27a61bc3dda36ebe28887d4da16df2093bbbcd8d58cfc87b2b48b70801c411;

        Session =         {
            id = ca77dd997db2169908a1a84204c99ae7;
        };

        SmugVault = 0;

        User =         {
            DisplayName = "smug";
            NickName = better1;
            id = 2455641;
        };

    };
    method = "smugmug.login.withPassword";
    stat = ok;

}

I want to get the display name. I am able to extract sessionId but not name. Please help me with this.

Bellow is the code

func postDataToURL() {

    let userName = self.userName.text;
    let userPassword = self.password.text;

    let postUrl = NSURL(string: "http://api.smugmug.com/hack/json/1.2.0/?method=smugmug.login.withPassword")!

    let smugUrlRequest = NSMutableURLRequest(URL: postUrl)

    smugUrlRequest.HTTPMethod = "POST"

    let postString = "&EmailAddress=" + userName! + "&Password=" + userPassword! + "&APIKey=1WcKTxQH2fegQTF0C7cVAORjeoTSD68V"

    smugUrlRequest.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(smugUrlRequest) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            print("ERROR")
        }

        if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")

        do {
            if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
                if let stat = json["method"] as? String {
                    print(stat) // London
                }

                print(json)

                let user = json["Login"]!
                let FileSizeLimit = user["FileSizeLimit"]
                print(FileSizeLimit)

                let PasswordHash = user["PasswordHash"]
                print(PasswordHash)

                let Session = user["Session"]
                print(Session)

                let DisplayName = user["User"]
                print(DisplayName)


            }
        } catch let error as NSError{
            print("\(error)")
        }


    }
    task.resume()
}
  • 3
    Possible duplicate of [How do I parse an array inside parsed JSON in Swift?](http://stackoverflow.com/questions/24026305/how-do-i-parse-an-array-inside-parsed-json-in-swift) – Mr. Bond Apr 29 '16 at 06:45
  • show your code how you managed to get the sessionId. to get the DisplayName should be very similar – Fonix Apr 29 '16 at 06:46
  • Possible duplicate of [How to convert a JSON string to a dictionary?](http://stackoverflow.com/questions/30480672/how-to-convert-a-json-string-to-a-dictionary) – 4oby Apr 29 '16 at 06:47
  • I have added the code. – better.sayali Apr 29 '16 at 06:53
  • 1
    There's no array in sight anywhere in your JSON. – gnasher729 Apr 29 '16 at 07:56

1 Answers1

0

Issue solved. solution is

   let jsonData = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMappedIfSafe)
            do {
                let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                print("jsonResult : ",jsonResult)
                print("Session : ",jsonResult.valueForKey("Login")?.valueForKey("Session"))
                print("DisplayName : ",(jsonResult.valueForKey("Login")?.valueForKey("User")?.valueForKey("DisplayName"))!)

            } catch {

}