我想做的是在网络上读取一个 json 文件(我知道与这方面的连接正在工作),然后获取一些文件内容以显示在我的 tableView 上。
我在另一个例子中能够很好地做到这一点,但我不确定现在的问题是什么。
JSON 文件:
{
"entry":
[
{"Current Date":"Tuesday June 22 2011","Time Period":"00:00 - 06:45,"},
{"Current Date":"Tuesday June 23 2011","Time Period":"00:00 - 07:22"}
]
}
从网络获取 JSON 文件的按钮:
- (IBAction)getJsonButton:(id)sender {
[array removeAllObjects];
NSURL *url = [NSURL URLWithString:@"https://LinkToWhereJSONfileIsLocated/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webData = [[NSMutableData alloc]init];
}
}
这是我认为我遇到问题的地方(一定是我的逻辑或语法):
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
NSArray *arrayOfEntry = [allDataDictionary objectForKey:@"entry"];
for (NSDictionary *diction in arrayOfEntry) {
NSString *currDate = [diction objectForKey:@"Current Date"];
[array addObject:currDate];
}
[[self myTableView]reloadData];
}
最佳答案
这是一个快速的猜测,尝试在主线程上调用 reloadData
方法。
关于objective-c - iOS从web读取Json并显示到tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13432240/