我需要从远程服务器上的多个源解析多个 JSON 对象,并且需要使用单个解析的每个值填充 UITableView:
单个 JSON 源,例如 h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A1:
{
"id": 0001,
"main": {
"mainA": 100,
},
}
单个 JSON 源,例如 h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A2:
{
"id": 0002,
"main": {
"mainA": 200,
},
}
单个 JSON 源,例如 h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A3:
{
"id": 0003,
"main": {
"mainA": 300,
},
}
在这里,我分配并初始化包含单个 URL 的数组:
- (void)viewDidLoad {
// other stuff
arrayA = [[NSMutableArray alloc] initWithObjects:@"A1", @"A2", @"A3", nil]; //each object A1, A2...An is the single subdirectory of URL for JSON source I need to parse
}
解析方法:
- (void)LoadParse { // this method is called by a UIButton
main = [[NSMutableArray alloc] init];
for (int i=0; i < [arrayA count]; i++) {
NSURL *url = [NSURL URLWithString:
[NSString stringWithFormat:
@"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@",[arrayA objectAtIndex:i]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSMutableDictionary *arrayMain = [JSON valueForKey:@"main"];
[mainA addObject:[arrayMain objectForKey:@"mainA"]];
NSLog(@"%@",mainA);
[table reloadData]; // mainA objects populates rows of UITableView
[table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){
}];
[operation start];
}
}
NSLog(@"%@",mainA) 的结果;
1st parsing -> NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
2nd parsing -> (after a few seconds) NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
3rd parsing -> (after other few seconds) NSLog result = random (100-200-300 or 100-300-200 or 200-300-100 or 200-100-300 or 300-200-100 or 300-100-200)
...
错误:正如您所看到的,每次按下 UIButton 并开始解析时,我都会得到 mainA 对象的不同序列(然后在表行中),例如:在第一次解析之后,我期望什么?100-200-300 .. 而不是,我得到 100-300-200 或 300-200-100 或其他,所以在下一次解析中,每次加载 parseMethod 时,我都会得到 mainA 的随机序列,而我可以保证远程服务器上的 A1-A2-A3 mainA 对象分别为 100-200-300。那么,你能帮我吗,这里有什么问题?我头晕了,谢谢!