Ios 如何从服务器将数据存储在NSmutable数组中
我试图将数据从服务器存储到NSMutable数组,以便在表视图中将它们显示为新闻提要,如图所示。基本上就像推特新闻提要一样。我要做的是从NSMutable数组中的服务器获取数据,并使用该数组在我的表视图中显示。我不知道这样做是否正确。我尝试过静态添加,但效果很好,但我真的不知道如何动态添加,因为我是Objective C的新手。如果这个问题看起来很愚蠢,我很抱歉。提前谢谢 使用JSON解析数据:Ios 如何从服务器将数据存储在NSmutable数组中,ios,nsmutablearray,Ios,Nsmutablearray,我试图将数据从服务器存储到NSMutable数组,以便在表视图中将它们显示为新闻提要,如图所示。基本上就像推特新闻提要一样。我要做的是从NSMutable数组中的服务器获取数据,并使用该数组在我的表视图中显示。我不知道这样做是否正确。我尝试过静态添加,但效果很好,但我真的不知道如何动态添加,因为我是Objective C的新手。如果这个问题看起来很愚蠢,我很抱歉。提前谢谢 使用JSON解析数据: dispatch_queue_t jsonParsingQueue = dispatch_queue
dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
// execute a task on that queue asynchronously
dispatch_async(jsonParsingQueue, ^{
NSString *urlStr = @"YourURL";
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseStr = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSData * jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *tempResults = [NSMutableArray alloc];
NSError *jsonParsingError = nil;
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&jsonParsingError];
tempResults = jsonObject[@"posts"]; //Add the json key you would like to get
self.arrayToDisplay = [tempResults copy]; //copy them to your NSMutableArray
// some code on a main thread (delegates, notifications, UI updates...)
dispatch_async(dispatch_get_main_queue(), ^{
[self.myTableView reloadData];
});
});
来自服务器的数据是JSON格式或XML格式的?来自服务器的数据是JSONTANKS格式的,以便您快速响应……)