从Jason数据访问字符串和INT值

发布于 2025-01-30 05:51:17 字数 1504 浏览 3 评论 0原文

解析本地保存的JSON文件,将JSON数据存储在nsarray中,然后在id型中存储在表观中打印值,但如果我想在“ ID”之类的int值中崩溃,我还要在int int inture中崩溃,如果我想在自定义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;
    
}

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;
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文