我工作用Objective-C iOS应用程序,我几乎已经完成,除了一两件事:解析JSON和本地保存
我解析一个JSON成的tableview,我想将数据本地存储在应用程序中。但我不知道这样做的最好方法。每一行都有一个图像,一个标题,描述和一个链接。我有不同的JSON标签。 (我不想在每次打开应用程序时解析JSON,我想检查是否有新的东西添加到JSON中,然后再解析它。但现在我只是开心,如果我可以设法从本地保存来自JSON的数据)
我一直在努力这一段时间,我真的需要一些帮助。在应用程序准备好App Store之前,这是我需要修复的最后一件事。
不知道您是否需要查看任何代码,但是我仍然发布它,以便您可以更好地了解我正在做的事情。
代码解析JSON:
jsonData_ = [[NSMutableData alloc] init];
NSString *urlString = [NSString stringWithFormat:@"http://www.myUrl.se/json/%@.json", _whatState];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *urlReq = [NSURLRequest requestWithURL: url];
NSURLConnection *connection = [NSURLConnection connectionWithRequest: urlReq delegate:self];
-(void)connectionDidFinishLoading:(NSURLConnection*) connection{
//NSLog(@"Succeeded! Recieved %d bytes of data", [jsonData_ length]);
NSError *error;
NSArray *items = [NSJSONSerialization JSONObjectWithData: jsonData_ options: kNilOptions error: &error];
NSSortDescriptor *sortDesc = [[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES]copy];
NSMutableArray *sortedItems = [NSMutableArray arrayWithObject: sortDesc];
sortedItems = [[items sortedArrayUsingDescriptors: sortedItems] copy];
if(!error)
{
tableData_ = sortedItems;
tableImgs_ = [[NSMutableArray alloc] initWithArray: tableData_ copyItems: YES];
[self.tableView reloadData];
jsonIsFinishedLoading = TRUE;
}
else
{
NSLog(@"ERROR");
}
}
代码为我的tableview:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"listItem";
TSCustomCell *cell = (TSCustomCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
[cell setSelectedBackgroundView:selectedCellBgColorView];
if(jsonIsFinishedLoading)
{
NSDictionary *row = [tableData_ objectAtIndex: indexPath.row];
NSString *title = [row objectForKey:@"title"];
cell.url = [row objectForKey:@"url"];
UIFont *defaultFont = [UIFont fontWithName:@"Edmondsans-Bold" size:12.0];
[cell.listItemLbl setText: title];
[cell.listItemLbl setFont:defaultFont];
if([[tableImgs_ objectAtIndex:indexPath.row] isMemberOfClass: [UIImage class]])
{
[cell.listItemImage setImage: [tableImgs_ objectAtIndex: indexPath.row]];
}
else
{
UIActivityIndicatorView *loadingSymbol = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
loadingSymbol.frame = cell.listItemImage.frame;
[cell addSubview: loadingSymbol];
[loadingSymbol startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
NSString *url = [row objectForKey: @"image"];
if([url isEqualToString: @""])
{
url = @"http://www.myUrl.se/images/standard.png";
}
else
{
url = [row objectForKey: @"image"];
}
NSURL *imgUrl = [NSURL URLWithString: url];
NSData *imageData = [NSData dataWithContentsOfURL: imgUrl];
UIImage *image = [UIImage imageWithData: imageData];
if(image != nil)
{
[tableImgs_ replaceObjectAtIndex:indexPath.row withObject:image];
NSLog(@"tableImgs != nil");
}
else
{
NSLog(@"tableImgs = nil");
[cell.listItemImage setImage: [UIImage imageNamed:@"standard_transp.png"]];
}
dispatch_sync(dispatch_get_main_queue(), ^{
[loadingSymbol removeFromSuperview];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationNone];
});
}); //end dispatch_async
}
}
}
return cell;
}
谢谢!
AFNetworking会帮我解决我的问题,在本地存储数据吗? – Jojo 2013-04-05 13:16:27
是的,绝对是!!只需阅读文章 – 2013-04-05 13:25:30