I am using AFNetworking 2 to grab some json data from a server. It is employee data, and I have made an employee object and an employees NSMutableArray.
My service is working. If I spit out responseObject to the log I get 600 employees. What I am struggling with is how to get those into my employees NSMutableArray.
the responseObject has a type of id, meaning an object with no class, right? I tried to change the id to employee, since that is what I want to return but that didn't work. Then I noticed the responseObject was actually ALL of the objects, so I tried employees but that didn't work.
So I thought I could loop through the objects, but since it isn't really an NSMutableArray I don't understand how.
Any help would be greatly appreciated.
Bryan
- (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];
}
This is inside my success bloc:
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);