我要做的是在网络上读取一个 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"}
]
}
从 Web 获取 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];
}