Nasir Khan
Nasir Khan

Reputation: 881

How to break json response data into multiple strings?

I want to get JSON response in separate string form, here is my code

NSString *myRequestString = [NSString stringWithFormat:@"register=yes&email=%@&fname=%@&lname=%@&password=%@&birthDate=%@&zip=%@",txt_email.text,txt_firstname.text,txt_lastname.text,txt_pass.text,txt_dob.text,txt_adress.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"URL"]];
  // set Request Type
  [request setHTTPMethod: @"POST"];
// Set content-type
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
//Log Response
    NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

And this is how i am getting in response.

The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-05-13 14:39:54.597 Traquer[5594:207956] {
            "message": { "email": "email is required " , "fname": "fname is required " , "lname": "lname is required " , "password": "password is required " , "birthDate": "birthDate is required " , "zip": "zip is required "  },
            "boolean": false,
            "eType": 0
    }

I want my message, boolean, etype and other data in seperate strings.

Upvotes: 1

Views: 488

Answers (2)

Ashok Londhe
Ashok Londhe

Reputation: 1491

Try it..

Code:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSArray *message=[dict objectForKey:@"message"];
BOOL boolean=[[dict  objectForKey:@"boolean"]boolValue];
NSString  *eType=[dict  objectForKey:@"eType"];

Note: returnData is the responce data coming form webservice.

Upvotes: 3

Halpo
Halpo

Reputation: 3134

Like this:

 NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];

 NSDictionary *message = responseDict[@"message"];

 NSString *email = message[@"email"];


 BOOL aBoolean = [responseDict[@"boolean"] boolValue];

 int eType = [responseDict[@"eType"] intValue];

Upvotes: 0

Related Questions