如何在JSON响应中从NSDictionary获取asosciated id

时间:2015-11-05 15:45:39

标签: objective-c json api afnetworking

我正在使用AFNetworking来解析JSON响应,我想知道如何从NSDictionary中获取值?

基本上,iOS应用使用AFJSONRequestOperationlocalhost:3000/api/csv_files检索JSON响应,并将请求的结果放入NSArray中。

我正在使用以下代码段来完成上述声明。

__weak typeof(self) weakSelf = self;
    AFJSONRequestOperation *operation =
        [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                            NSDictionary *jsonDict = (NSDictionary *) JSON;
                                                            // this is the array that stores the JSON response
                                                            NSArray *csvFiles = [jsonDict objectForKey:@"csv_files"];
                                                            [csvFiles enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop){
                                                                NSString *csvFileFilename = [obj objectForKey:@"csv_file_filename"];
//                                                              NSLog(@"CSV Filenames:%@",csvFileFilename);
                                                                [weakSelf processJSONResponse:csvFiles];
                                                                                            }];


                                                    }   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                            NSLog(@"Request Failure Because %@",[error userInfo]);
                                                                                            }];
    [operation start];

现在我正在尝试处理JSON响应,以便设备的UUID将存储在其中一个上传文件的文件名中的UUID与API匹配。我用下面的代码片段完成了这个,

- (void)processJSONResponse:(NSArray *) csvFiles {

    NSLog(@"CSV Files array:%@",csvFiles);

    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

    NSString *fileName = [NSString stringWithFormat:@"KegCop-users-%@.csv",idfv];

    BOOL hasString = NO;
    for (NSDictionary *fileInfo in csvFiles) {
        if ([fileInfo[@"csv_file_filename"] isEqualToString:fileName]) {
            hasString = YES;
            break;
        }
    }
    NSLog(@"%hhd",hasString);
}

现在我在csvFiles NSArray中找到带有正确文件名的附带文件后,如何找到关联的id属性?

JSON响应如下所示, JSON response

2 个答案:

答案 0 :(得分:0)

- (NSInteger)getIDFromCSVArray:(NSArray *) csvFiles {

    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    for (NSDictionary *fileInfo in csvFiles) {
        if ([fileInfo[@"csv_file_filename"] containsString:idfv]) {
            return [[fileInfo[@"id"] integerValue];
        }
    }
    return -1;
}

答案 1 :(得分:0)

-(NSInteger)idOfCSVFile:(NSDictionary*)csvFileDict
{
    NSNumber* idNumber = [csvFileDict objectForKey:@"id"];
    if (idNumber) return [idNumber integerValue];
    return -1; // I guess it should not happen...
}