我正在使用AFNetworking來解析JSON響應,並且我想知道如何從NSDictionary中獲取值?如何從JSON響應中的NSDictionary獲取asosciated ID
基本上,iOS應用程序使用AFJSONRequestOperation
從localhost: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存儲在文件上傳到API中的一個文件名中的UUID相匹配。而我做到這一點與下面的代碼片段,
- (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結果,你知道如何讓'csv_file_filename',你爲什麼不似乎知道如何獲得'csv_file_id'值?這是你的問題嗎?請澄清你真正需要幫助的東西。 – rmaddy
@rmaddy我需要幫助獲取'id',在這種情況下是'85'而不是csv_file_id。我不確定在找到基於文件名的正確文件後,如何獲得'id'。 – Chris
'fileInfo [@「id」]'。 – rmaddy