在Objective-C中解析JSON之后的随机结果 [英] Random results after parsing JSON in Objective-C

查看:174
本文介绍了在Objective-C中解析JSON之后的随机结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从远程服务器上的多个来源解析多个JSON对象,并且需要用单个解析的每个值填充UITableView:

I need to parse multiple JSON objects from several sources on a remote server and I need to populate UITableView with each value of the single parse:

单个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.那么,你能帮我吗,这是怎么了?我迷失了方向,谢谢!

THE ERROR: as u can see every time I press the UIButton and start parsing I get a DIFFERENT SEQUENCE of mainA objects (then in the table rows), example: after the first parsing, what do I expect? 100-200-300.. and instead no, I get 100-300-200 or 300-200-100, or others, and so in the next parsing, every time I load the parseMethod I get random sequence of mainA, while I can assure that on the REMOTE server A1-A2-A3 mainA objects are respectively 100-200-300. So, can u help me, what is wrong here? I'm losing my head, thanks!

推荐答案

每次进行循环时,都会向服务器发出一个 asynchronous 请求.每个请求都有一个完成块,当结果从Web服务器返回时将执行该块,但是由于它们是异步的,因此您不知道它们的执行顺序(无论哪个服务器响应首先到达,都将在其完成块中首先进行处理)

Each time you proceed through the loop, you issue an asynchronous request to the server. Each request has a completion block which is executed when the result arrives back from the webserver, but because these are asynchronous, you have no idea the order they will be executed in (whichever server response arrives first will be processed first in it's completion block).

要解决此问题,您可以在声明数组时预先填充它:

To fix this you can pre-fill your array when you declare it:

    main = [@[@"A",@"B",@"C"] mutableCopy];

然后在循环中,您可以将每个响应分配给它在数组中的相应位置:

Then in your loop you can allocate each response to it's respective position in the array:

        [main replaceObjectAtIndex:i withObject:[arrayMain objectForKey:@"mainA"]];

您应该预先填充数组,因为您不能以大于数组现有长度的索引添加对象.

You should pre-fill the array as you cannot add an object at an index greater than the existing length of the array.

edit 我不清楚您的代码中arrayA(您已预先填充),mainmainA之间的关系,但是我敢肯定,您可以原理.

edit I am not clear the relationship in your code between arrayA (which you do pre-populate), main, and mainA, but I am sure you can get the principle.

这篇关于在Objective-C中解析JSON之后的随机结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆