从Jason数据访问字符串和INT值
解析本地保存的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论