-2

I am using NSURLSession and NSURLSessionDataTask to send a post request and then get the response and store it in a variable that is an NSString and then log it. My function right now looks like this:

 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) { NSLog(@"Response:%@ %@\n", response, error); if(error == nil) { NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog(@"Json = %@",text); } }]; [dataTask resume]; 

When I run the request it returns the following in the debugging terminal:

Json = {"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"} 

How can I know parse this using NSJSONSerialization? Can this even be parsed or do I have some formatting to do on the php? This is my first web service.

0

2 Answers 2

1

NSJSONSerialization uses NSData not NSString.

Replace your line...

NSString *text = [[NSString alloc] initWithData... 

With something like this...

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

The dictionary object will now be a representation of the JSON.

This depends on the JSON being correct though :-)

Also, this has been asked hundreds of times before.

Sign up to request clarification or add additional context in comments.

1 Comment

I am getting Dictionary is null
0
NSMutableDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.