Mishal Awan Mishal Awan - 5 months ago 22
JSON Question

NSString is coming nil

I am new to iOS. Its my first app in which I am calling JSON date from server.
I have to store coming values in tabkeviewcell, but when I am setting value in NSString it is getting nil.
Below is my code :-
.h file

@interface SecondViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(strong,nonatomic) NSMutableDictionary *jsonresponse;
@property(strong, nonatomic) NSMutableArray *jsonresultarr;


.m file

- (void)viewDidLoad {
[self retrievedata];

[super viewDidLoad];
// Do any additional setup after loading the view.
}

-(void) retrievedata
{
//NSMutableArray *jsonresultarr=[NSMutableArray new];

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]]; // this is your request url

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; // here parsing the array

NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

for (int i=0; i<[firstArry count]; i++)
{
_jsonresponse= [firstArry objectAtIndex:i];

NSString *Company_Id=[_jsonresponse objectForKey:@"Company_Id"];
NSString *Company_Name=[_jsonresponse objectForKey:@"Company_Name"];
NSString *Company_Address=[_jsonresponse objectForKey:@"Company_Address"];
NSString *Company_Email=[_jsonresponse objectForKey:@"Company_Email"];
NSString *Company_Tel_Num=[_jsonresponse objectForKey:@"Company_Tel_Num"];
NSString *Company_Website=[_jsonresponse objectForKey:@"Company_Website"];
NSString *Company_Fax_Num=[_jsonresponse objectForKey:@"Company_Fax_Num"];
[_jsonresultarr addObject:Company_Id];
[_jsonresultarr addObject:Company_Name];
[_jsonresultarr addObject:Company_Address];
[_jsonresultarr addObject:Company_Email];
[_jsonresultarr addObject:Company_Tel_Num];
[_jsonresultarr addObject:Company_Website];
[_jsonresultarr addObject:Company_Fax_Num];







}
[_tableview reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(self.jsonresultarr) // coming nil
{ return _jsonresultarr.count;

}
return 7;
}


Here is my output response. Can anyone please give me an example or try to modify my code so that I can store data in text boxes.

[{"status":"Y"},{"company_data":[{"Company_Id":"5","Company_Name":"SANTE (PVT) LIMITED","Company_Address":"245\/2-Z P.E.C.H.S Block-6 \nKarachi-75400,Pakistan","Company_Email":"sante@sante.com.pk","Company_Tel_Num":"4520507, 4533425","Company_Website":"www.sante.com.pk","Company_Fax_Num":"4.54885e+006"}

Answer
 NSMutableArray *jsonresultarr=[NSMutableArray new];   // declare this array in globally

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://magmasysdev.com/ddc/getAllCompanies.php"]];  // this is your request url

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];     // here parsing the array

NSArray *firstArry=[[jsonArray objectAtIndex:1]objectForKey:@"company_data"];

for (int i=0; i<[firstArry count]; i++)
{
    NSMutableDictionary *getjsonresponse= [firstArry objectAtIndex:i];

    NSString *Company_Id=[getjsonresponse objectForKey:@"Company_Id"];
    NSString *Company_Name=[getjsonresponse objectForKey:@"Company_Name"];
    NSString *Company_Address=[getjsonresponse objectForKey:@"Company_Address"];
    NSString *Company_Email=[getjsonresponse objectForKey:@"Company_Email"];
    NSString *Company_Tel_Num=[getjsonresponse objectForKey:@"Company_Tel_Num"];
    NSString *Company_Website=[getjsonresponse objectForKey:@"Company_Website"];
    NSString *Company_Fax_Num=[getjsonresponse objectForKey:@"Company_Fax_Num"];


}

another choice

     for (NSDictionary *tmp in firstArry)
        {
          NSMutableDictionary * itemshowdetails=[[NSMutableDictionary alloc]init];

            [itemshowdetails setValue:[tmp objectForKey:@"Company_Id"] forKey:@"Company_Id"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Name"] forKey:@"Company_Name"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Address"] forKey:@"Company_Address"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Email"] forKey:@"Company_Email"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Tel_Num"] forKey:@"Company_Tel_Num"];
            [itemshowdetails setValue:[tmp objectForKey:@"Company_Website"] forKey:@"Company_Website"];

              [itemshowdetails setValue:[tmp objectForKey:@"Company_Fax_Num"] forKey:@"Company_Fax_Num"];


            [jsonresultarr addObject:itemshowdetails];



        }

in your retrieve

  NSString *pass = [[jsonresultarr objectAtIndex:0] objectForKey:@"Company_Id"];  // here pass your index if already I know else call the line inside the for loop



#pragma mark - tablevie data source

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [jsonresultarr count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
   .....

   // here pass your details

 cell.Company_Id.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Id"]; 

 cell.Company_Name.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Name"]; 

 cell.Company_Address.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Address"];

  cell. Company_Email.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Email"]; 

  cell.Company_Tel_Num.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Tel_Num"]; 

 cell.Company_Website.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Website"]; 
 cell.Company_Fax_Num.text =  [[jsonresultarr objectAtIndex:indexPath.row ] objectForKey:@"Company_Fax_Num"]; 


return cell;

}

Comments