sandeep tomar sandeep tomar - 5 months ago 14
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 = @"http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=registration";
[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,

JSON

result : "Successful"
data
0
user_id : "1"
email_id : "gupta.monu4@gmail.com"
password : "monu"
phone_no : ""
user_type : "admin"
company_name : "Brainpulse"


This is my json structure.

Answer

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

     }
     else
     {
       // show the failure report
      }
}
else
{
 // show the failure report
}
Comments