RedShirt
RedShirt

Reputation: 864

NSURLSession: Get Json from NSURLResponse

I am trying to figure out how to parse the JSON from my http response. Currently I am using a NSURLSession task to send a post request and get the response and results like the following:

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
     NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

     NSURL * url = [NSURL URLWithString:@"http://mysite.org/mobile_app/studentregister.php"];
     NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
     NSString * params = [NSString stringWithFormat:@"username=%@&displayname=%@&password=%@&passwordc=%@&email=%@&teachercode=%@", username, displayname, password, passwordc, email, teacherCode];
     [urlRequest setHTTPMethod:@"POST"];
     [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

     NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
     completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
     NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
     NSError *serializeError = nil;
     NSDictionary *jsonData = [NSJSONSerialization
                                   JSONObjectWithData:data
                                   options:NSJSONReadingMutableContainers
                                   error:&serializeError];

     NSLog(@"Response:%@ %@\n", response, error);
     if(error == nil)
     {
     NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
     NSLog(@"Data = %@",text);
     }

     }];

Now when I run the request and log the following response:

{"userCharLimit":"Your username must be between 5 and 25 characters in length"}{"displaynameCharLim":"Your displayname must be between 5 and 25 characters in length"}{"passLimit":"Your password must be between 8 and 50 characters in length"}{"emailInvalid":"Not a valid email address"}{"teacherCodeLength":"Your teacher code is not 5 to 12 characters"}
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />

But how can I parse just the:

{"userCharLimit":"Your username must be between 5 and 25 characters in length"}{"displaynameCharLim":"Your displayname must be between 5 and 25 characters in length"}{"passLimit":"Your password must be between 8 and 50 characters in length"}{"emailInvalid":"Not a valid email address"}{"teacherCodeLength":"Your teacher code is not 5 to 12 characters"} in ios?

Upvotes: 1

Views: 2173

Answers (1)

Duncan C
Duncan C

Reputation: 131501

Take a look at the NSJSONSerialization class. You'd pass it the data from your URL Connection and get back an "object graph" of foundation objects. In your case it looks like you'd get a dictionary.

Better, though, is to use AFNetworking. It's available on Github or CocoaPods, and makes all this stuff really easy.

Upvotes: 2

Related Questions