-3

我需要再次解析来自远程服务器上多个源的多个 JSON 字符串,并且我需要使用单个解析的每个值填充 UITableView:感谢@He之前的答案。

单个 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,
  },
}

目标:解析 mainA 的每个值并以正确的顺序获取它们 A1->A2->A3

我们可以解析远程 JSON 字符串的单个 URL 数组:

idURL = [[NSArray alloc] initwithObjects:@"A1", @A2", @A3", nil];

解析,感谢@Sulthan,我需要块,因为我必须等待 END 调用 theGreatMethod:

- (void)parsingJSON { // starts with a UIButton pressed!

    mainA = [@[@"1",@"2",@"3"] mutableCopy]; // defined here to preserve right order of mainA objects after parsing

    int mainACount = 0;

    typedef void (^AFJSONSuccessBlock) (NSURLRequest *request, NSHTTPURLResponse *response, id JSON);
    typedef void (^AFJSONFailureBlock) (NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON);

    __block int numRequests = [idURL count];

    AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        NSMutableDictionary *dictMain = [JSON objectForKey:@"main"];
        [mainA replaceObjectAtIndex:mainACount withObject:[dictMain objectForKey:@"mainA"]];

        mainACount ++; // EVERY AFJSONRequestOperation *operation int mainACount = mainACount + 1 
        numRequests --; // EVERY AFJSONRequestOperation *operation int numRequest = numRequest - 1

        if (numRequests == 0) {

            NSLog(@"END PARSING");
            NSLog(@"FINAL mainA ORDER: %@",mainA);

        [self theGreatMethod]; // a method to populate UITableView with mainA results

        }
    };

    AFJSONFailureBlock onFailure = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        NSLog(@"ERROR: %@", [error userInfo]);

        numRequests--;

        if (numRequests == 0) {

         NSLog(@"ERROR PARSING");
            [self theGreatMethod]; // a method to populate UITableView with mainA results

        }
    };

    for (NSString* jsonPath in idURL) {

        NSString* absolutePath = [NSString stringWithFormat:@"h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@", jsonPath];
        NSURL *url = [NSURL URLWithString:absolutePath];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        AFJSONRequestOperation *operation;
        operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                    success:onSuccess
                                                                    failure:onFailure];
        [operation start];

    }

        NSLog(@"START PARSING");

}

我在这种情况下定义了一个int mainACount来计算每个 AFJSONRequestOperation *操作周期并逐个替换 mainA 对象,但我不确定这是正确的方法;我也尝试过 (int i=0; i < [mainA count]; i++) 和 [mainA replaceObjectAtIndex:i] 循环,但我有相同的错误结果。

NSLog(@"FINAL mainA ORDER: %@",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,而不是随机的......所以,你能再帮我一次吗,这里有什么问题?或者,是否有更好的方法以正确的顺序进行解析?

4

1 回答 1

1

尝试像这样声明 URL 数组:

idURL = [[NSArray alloc] initwithObjects:@"A1", @"A2", @"A3", nil];

即使for循环按顺序解析对象,当您使用时,也不能保证NSArray调用返回结果的顺序,因为它是异步请求。AFJSONRequestOperation

您可能希望使用@synchronized 指令锁定线程:

- (void)parsingJSON:(id)anObj
{
    // ...
    AFJSONSuccessBlock onSuccess = ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        // ...
        @synchronized(anObj)
        {
            // Everything between the braces is protected by the @synchronized directive.
            var2 ++; // EVERY AFJSONRequestOperation *operation int var2 = var2 + 1 
            numRequests --; // EVERY AFJSONRequestOperation *operation int numRequest = numRequest - 1
        }
        // ...
    }
    // ...
}
于 2013-02-07T13:06:20.850 回答