sandeep tomar sandeep tomar - 1 year ago 107
JSON Question

how to get response of json data in objective c?

I am posting my data in url with this block of code.

NSString *urlString = [NSString stringWithFormat:@"company_name=%@&email_id=%@&password=%@",_CompanyName.text,_Email.text, _Password.text];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[urlString length]];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc]init];
NSString *url = @"";
[req setURL:[NSURL URLWithString:url]];
[req setHTTPMethod:@"POST"];
[req setValue:postLength forHTTPHeaderField:@"Content-Length"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setHTTPBody:[urlString dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:
^(NSURLResponse *reponse , NSData *data, NSError *error){
NSError *errre1;
dictLogin=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&errre1];
NSLog(@"result is%@ :",dictLogin);
NSLog(@"result is%@ :",urlString);
[user setObject:[dictLogin objectForKey:@"company_name"] forKey:@"company_name"];
[user setObject:[dictLogin objectForKey:@"email_id"] forKey:@"email_id"];
[user setObject:[dictLogin objectForKey:@"passord"] forKey:@"password"];

Now i don't know ho to get response when i am going to login screen,


result : "Successful"
user_id : "1"
email_id : ""
password : "monu"
phone_no : ""
user_type : "admin"
company_name : "Brainpulse"

This is my json structure.

Answer Source

check like this is avoid the crash and memory leaks

  // create the one global array for save
  NSMutableArray *user =[NSMutableArray new];

dictLogin=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&errre1];

if (dictLogin)
// serilize your data
    if ([[dictLogin objectForKey:@"result"] isEqualToString:"Successful"])

        [user removeAllobjects];
       // assign into one array
        NSArray *TempArray = dictLogin[@"data"];

        for (NSDictionary *tmp in TempArray)
          // careate one temporary dictionary
           NSMutableDictionary *tmpDict = [NSMutableDictionary new];

      // store your data to Dictionary or array
        [tmpDict setObject:[tmp objectForKey:@"company_name"] forKey:@"company_name"];
         [tmpDict setObject:[tmp objectForKey:@"email_id"] forKey:@"email_id"];

        if ([tmp objectForKey:@"passord"] != nil) {
         [tmpDict setObject:[tmp objectForKey:@"passord"] forKey:@"password"];

            [user addobject:tmpDict];

            // then use your data where u need

       // show the failure report
 // show the failure report
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download