我正在使用AFNetworking 2從服務器獲取一些json數據。這是員工數據,我創建了一個員工對象和一個員工NSMutableArray。使用AFNetworking解析JSON
我的服務正在運行。如果我吐出響應對象的日誌,我會得到600名員工。我正在努力的是如何讓這些人進入我的員工NSMutableArray。
responseObject有一個id類型,意思是沒有類的對象,對嗎?我嘗試將ID更改爲員工,因爲這是我想要返回的,但這並不起作用。然後我注意到responseObject實際上是所有的對象,所以我嘗試了員工,但沒有奏效。
所以我想我可以通過對象循環,但因爲它不是一個真正的NSMutableArray,我不明白如何。
任何幫助將不勝感激。
布萊恩
- (IBAction)jsonTapped:(id)sender
{
NSMutableArray *employees;
employee *thisEmployee = [employee new];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"XXXXXX" password:@"XXXXXX"];
AFHTTPRequestOperation *operation = [manager GET: @"https:/XXXXXXXXXXX/something"
parameters: [self jsonDict]
success:^(
AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Submit response data: %@", responseObject);}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);}
];
[operation start];
}
這是我成功集團內部:
NSMutableArray *employees = (NSMutableArray *)responseObject;
NSLog(@"Count of array to begin: %lu", (unsigned long)[employees count]);
NSLog(@"JSON RESULT %@", responseObject);
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
for (employee *thisemployee in employees) {
BOOL success = [db executeUpdate:@"INSERT INTO employees (firstName,fullName,email) VALUES (?,?,?,?);",thisemployee.firstName,thisemployee.lastName,thisemployee.fullName,thisemployee.emailAddress, nil];
if (success) {} // Only to remove success error
NSLog(@"DDD %@", thisemployee);
響應對象將是NSArray或NSDictionary,具體取決於指定的JSON。您需要將其轉換爲正確的類型才能訪問數據。 –