user3432634
user3432634

Reputation: 1

Accessing string and int values from JASON data

Parsing a JSON file saved locally,storing JSON data in NSArray and further NSArray storing in id type to print values in table view but getting crash in getting int value like "id" also what I have to do if I wants to display different values in custom TableViewCell.


  -(void)jasonParsing
{
    
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"EmployeeData" ofType:@"json"];

        NSError * error;
        NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];


        if(error)
        {
            NSLog(@"Error reading file: %@",error.localizedDescription);
        }

//@property NSArray *dataList; defined in (.m)
        self.dataList = (NSArray *)[NSJSONSerialization
                                    JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                    options:0 error:NULL];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
   

    if (cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    id keyValuePair =self.dataList[indexPath.row];
//by using below code crashing with error([__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)
    cell.textLabel.text = keyValuePair[@"id"];
//by using below code values are printing all the name
    cell.textLabel.text = keyValuePair[@"name"];


        
        return cell;
    
}

Upvotes: 0

Views: 92

Answers (0)

Related Questions