如何使用来自一个JSON请求的数据进行第二次JSON请求?

时间:2013-06-17 21:20:10

标签: ios objective-c json parsing uitableview

我成功地从API解析JSON并在tableview中显示它。我收到的数据是他注册的用户类,但我收到的数据只是类ID,因此我必须获取每个类ID并发送单独的API请求以获取类名和教师。我能够在单独的视图控制器上成功执行此操作,因为如果用户单击该行,我可以获取类ID,但是,我想在单个表视图上显示类名,教师和ID。

所以我的问题是,如何从我的表视图中获取数据并为每个单元格单独生成api请求并将数据显示在该单元格上?

我目前的数据:

What I have so far

如果我点击一行,我会得到这些数据:

2013-06-17 17:06:59.232 App[22024:c07] Class: Honors Pre-Calculus
2013-06-17 17:06:59.233 App[22024:c07] Teacher: Joe Shmo

如何在一个屏幕上显示两者?

这是我在第一个屏幕上检索类ID的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if ([searchResults count] == 0) {
    return [tableView dequeueReusableCellWithIdentifier:
            NothingFoundCellIdentifier];



} else {

    ScheduleResultCell *cell = (ScheduleResultCell *)[tableView dequeueReusableCellWithIdentifier:SearchResultCellIdentifier];

    ScheduleResult *searchResult = [searchResults objectAtIndex:indexPath.row];


    NSString *classFormat = @"%@";
    NSString *classfks = searchResult.class_fk;
    NSString *classfk = [NSString stringWithFormat:classFormat, classfks];

    cell.idLabel.text = classfk;

    return cell;

}
}

这是我在第二个屏幕上检索课程信息的代码:

-(void)viewDidAppear:(BOOL)animated {
url = [NSString stringWithFormat:@"https://myapiurl.com/v1/classes/%@.json", idValue];

//show loader view
[SVProgressHUD showWithStatus:@"Loading Info"];

//fetch the feed
_feed = [[ClassFeed alloc] initFromURLWithString:url
                                      completion:^(JSONModel *model, JSONModelError *err) {

                                          //hide the loader view
                                          [SVProgressHUD dismiss];

                                          //json fetched
                                          NSLog(@"Class: %@", _feed.course);
                                          NSLog(@"Teacher: %@", _feed.teacher_full_name);
                                          NSLog(@"Meeting Info: %@", _feed.meeting_times);

                                          [self.tableView reloadData];

                                      }];

}

知道如何在一个屏幕上完成此操作吗?

以下是我一直在尝试做的事情,但似乎只显示一个单元格的数据:

int count = [searchResults count];

    NSString *classFormat = @"%@";
    NSString *classfks = searchResult.class_fk;
    NSString *classfk = [NSString stringWithFormat:classFormat, classfks];

    for (int i=0; i < count; i++) {
        ScheduleResult *classInfo = [searchResults objectAtIndex:indexPath.row];
        NSString *classId = classInfo.class_fk;
        NSLog(@"i value: %d", i);
        url2 = [NSString stringWithFormat:@"https://apiurl.com/v1/classes/%@.json", classId];
        NSLog(@"url value: %@", url2);
        [SVProgressHUD showWithStatus:@"Loading Info"];
        _feed = [[ClassFeed alloc] initFromURLWithString:url2
                                              completion:^(JSONModel *model, JSONModelError *err) {

                                                  //hide the loader view
                                                  [SVProgressHUD dismiss];

                                                  //json fetched
                                                  NSLog(@"Class #%d: %@", i, _feed.course);
                                                  NSLog(@"Teacher #%d: %@", i,  _feed.teacher_full_name);
                                                  NSLog(@"Meeting #%d: %@", i,  _feed.meeting_times);
                                                  //name = namefk;
                                                  NSString *nameFormat = @"%@";
                                                  NSString *namefks = _feed.course;
                                                  NSString *namefk = [NSString stringWithFormat:nameFormat, namefks];
                                                  cell.nameLabel.text = namefk;

                                                  //[self.tableView reloadData];

                                              }];



                }

我尝试将数据添加到数组中以尝试不同的方法,但我甚至无法正确完成。

NSMutableArray* fullArray;
    fullArray = [[NSMutableArray alloc] init];
    [fullArray addObject:classfk];
    NSLog(@"array: %@", fullArray);

调试输出:

array: ( 10326
) 2013-06-17 23:36:45.488 App[30088:c07] array: ( 10371 ) 
2013-06-17 23:36:45.494 App[30088:c07] array: (
10420 )

如果这个问题没有多大意义,我想问一下如何从这个JSON提要(第一个网址)获取class_fk:

[{
    "class_fk": 10326,
    "currently_enrolled": true,
    "date_withdrawn": null,
    "enrollment_pk": 147745,
    "fee_paid": false,
    "late_date_enrolled": null,
    "level": null,
    "student_fk": 132664,
    "update_date": "2012-08-27"
},
{
    "class_fk": 10371,
    "currently_enrolled": true,
    "date_withdrawn": null,
    "enrollment_pk": 147168,
    "fee_paid": false,
    "late_date_enrolled": null,
    "level": null,
    "student_fk": 132664,
    "update_date": "2012-08-23"
}

并使用它插入另一个网址并从中获取课程和teacher_full_name? (第二个网址):

{
"class_id": "ADVMTG-40",
"class_pk": 10326,
"course": "Advisor Meeting",
"course_id": 7,
"course_type": "Advisory",
"description": "ADV: Shmo, J.",
"group_fk": 13980,
"primary_grade_level": "None",
"school_level": "Upper School",
"school_year": 2012,
"status": "Active",
"subject": "Administration",
"teacher_fk": 80404,
"teacher_full_name": "Joe Shmo",
"update_date": "2012-10-23T10:06:00-05:00",
"teachers": [
    {
        "person_fk": 80404,
        "role": "Primary Teacher",
        "role_id": 1,
        "teacher_name": "Joe Shmo",
        "update_grades": true,
        "view_grades": true
    }
],
"meeting_times": [
    {
        "block": "Advisor Meetings",
        "block_abbreviation": "ADV",
        "day": "Friday",
        "end_time": null,
        "grading_period": "ALL",
        "room": null,
        "start_time": null
    }
]}

这是我的问题的解决方案(非常感谢你的帮助!):

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{



    //[searchResults sortUsingSelector:@selector(compareName:)];

    NSString *urlString = @"https://apiurl.com/v1/enrollments.json?student=132664";
    NSURL *Url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:Url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSArray *classes = [NSJSONSerialization JSONObjectWithData:data
                                                       options:NSJSONReadingMutableContainers
                                                         error:nil];
    //NSLog(@"classes array: %@", classes);

    //now we loop through all classes
    for (NSMutableDictionary *class in classes) {

        //we get individual data for each class

        NSString *classID = class[@"class_fk"];
        //NSLog(@"classID: %@", classID);
        NSString *classUrlString = [NSString stringWithFormat:@"https://apiurl.com/v1/classes/%@.json", classID];
        NSURL *classUrl = [NSURL URLWithString:classUrlString];
        NSURLRequest *classRequest = [NSURLRequest requestWithURL:classUrl];


        NSData *classData = [NSURLConnection sendSynchronousRequest:classRequest returningResponse:nil error:nil];
        NSDictionary *classDictionary = [NSJSONSerialization JSONObjectWithData:classData
                                                                        options:NSJSONReadingMutableContainers
                                                                          error:nil];
        //NSLog(@"classes dictionary: %@", classDictionary);
        if (classes == nil) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showNetworkError];
            });
            return;

        } else {
            if (classDictionary == nil) {
                NSLog(@"classes dictionary: %@", classDictionary);
            } else {
        //probably should check here if array is empty before accessing first object
        //we shove the data from a classDictionary into class
        //class[@"teacher_full_name"] = classDictionary[@"teacher"][0];

                if (classDictionary[@"teacher_full_name"] == nil) {
                    class[@"teacher_full_name"] = @"Not Provided";
                } else {
                    class[@"teacher_full_name"] = classDictionary[@"teacher_full_name"];
                }

                if (classDictionary[@"course"] == nil) {
                    class[@"course"] = @"Not Provided";
                } else {
                    class[@"course"] = classDictionary[@"course"];
                }

                if (classDictionary[@"class_pk"] == nil) {
                    class[@"class_pk"] = @"Not Provided";
                } else {
                    class[@"class_pk"] = classDictionary[@"class_pk"];
                }

                if (classDictionary[@"teachers"][0][@"person_fk"] == nil) {
                    class[@"person_fk"] = @"Not Provided";
                } else {
                 class[@"person_fk"] = classDictionary[@"teachers"][0][@"person_fk"];   
                }

        ScheduleResult *searchResult = [[ScheduleResult alloc] init];
        searchResult.name = class[@"course"];
        searchResult.teacher = class[@"teacher_full_name"];
        searchResult.class_fk = class[@"class_pk"];

        [searchResults addObject:searchResult];


        NSLog(@"searchresult: %@", class[@"person_fk"]);
        //NSLog(@"course: %@ and teacher: %@", class[@"course"], class[@"teacher_full_name"] );
            }
        }

    }



    dispatch_async(dispatch_get_main_queue(), ^{
        //NSLog(@"dictionary: %@", classes);
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    });

1 个答案:

答案 0 :(得分:2)

好的,这里有两个选择。

首先是(在后台队列中),获取初始订阅源。然后解析JSON。然后遍历JSON并对数组中的每个字典进行api调用,并将其推回到字典中。然后告诉你的表重新加载主队列。

第二种选择稍微复杂一些。当用户滚动表格时,您可以在后台队列中进行调用以获取适当的数据并更新单元格。它与图像在单元格中异步加载的方式非常相似。除了这里,它不是图像而是JSON。你应该缓存这些数据,这样就不会发生两次,如果用户滚动得太快,加载数据的单元格不再出现在屏幕上,则取消请求。

我建议你留在第一个,保持简单和容易(虽然可能没那么高效,因为你渴望提前加载数据,但如果第一个api调用返回的记录数很少,那么它应该没问题,但是如果它是100秒,那么选择第二个选项)。

另外,需要注意的是,您需要在第一个选项中同步执行所有操作(这就是您知道所有数据的下载方式)。

好的,这是第一个选择。我是用NSURLConnection做到的。请注意,我不喜欢这个选项,但它很容易完成工作:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        //first we get all classes
        NSString *urlString = @"https://myapiurl.com/v1/classes";
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSArray *classes = [NSJSONSerialization JSONObjectWithData:data
                                                           options:NSJSONReadingMutableContainers
                                                             error:nil];

        //now we loop through all classes
        for (NSMutableDictionary *class in classes) {

            //we get individual data for each class

            NSString *classID = class[@"class_fk"];
            NSString *classUrlString = [NSString stringWithFormat:@"https://apiurl.com/v1/classes/%@.json", classID];
            NSURL *classUrl = [NSURL URLWithString:classUrlString];
            NSURLRequest *classRequest = [NSURLRequest requestWithURL:classUrl];


            NSData *classData = [NSURLConnection sendSynchronousRequest:classRequest returningResponse:nil error:nil];
            NSDictionary *classDictionary = [NSJSONSerialization JSONObjectWithData:classData
                                                                            options:NSJSONReadingMutableContainers
                                                                              error:nil];

            //probably should check here if array is empty before accessing first object
            //we shove the data from a classDictionary into class
            class[@"teachers_name"] = classDictionary[@"teachers"][0];
            class[@"course"] = classDictionary[@"course"];

        }

        //now you've got an array of dictionaries that have teachers name and course in it.
        //we tell our tableview to reload the data on main queue
        dispatch_async(dispatch_get_main_queue(), ^{
            [someTableView reloadData];
        });
    });