L
L
Lorem Ipsum2014-09-18 16:14:33
Objective-C
Lorem Ipsum, 2014-09-18 16:14:33

How to parse json server response in objective-c?

Good day to all!
I'm making an iOS client for the site and there was a problem.
I send a request to the server with an e-mail and a password.

//нажимаем на кнопку sign up
- (IBAction)enterButton:(id)sender {
    
    NSString *myEmail = textFieldEmail.text;
    NSString *myPassword= textFieldPassword.text;

    //url encoding email
    NSString *encodeEmail = myEmail;
    NSString *escapedStringEmail = [encodeEmail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    
    //md5 password
    NSString *md5Paswword = [NSString md5String:myPassword];
    
    NSString *url = [NSString stringWithFormat:@"http://site.com/app/api/user/auth/%@/%@", escapedStringEmail, md5Paswword];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
                                                                        URLWithString:url]
                                                                        cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
                                    request.HTTPMethod = @"POST";

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (connection) {
        
        receviedData = [[NSMutableData data] init];
        
    }
    
    NSLog(@"%@", request);
}

//возвращаем ответ авторизаци
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receviedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [receviedData appendData:data];
    
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    
    NSString *data = [[NSString alloc] initWithData:receviedData encoding:NSUTF8StringEncoding];
    
    UIAlertView *authResult = [[UIAlertView alloc] initWithTitle:@"Результат" message:data delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
    
    [authResult show];


}

The request is sent correctly. The server should return the response in json (Server response example {"result":true,"token":"aa7233c079ae32d62e97bf476bfd99f8"} )
How to parse json server response?
As far as I understand, I get a regular string in the form of a json array

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
S
smanioso, 2014-09-18
@smanioso

See the NSJSONSerialization class: https://developer.apple.com/library/ios/documentat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question