//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];//1
NSArray* latestLoans = [json objectForKey:@"loans"]; //2
NSLog(@"loans: %@", latestLoans); //3
// 1) Get the latest loan
NSDictionary* loan = [latestLoans objectAtIndex:0];
// 2) Get the funded amount and loan amount
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"]; NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] -
[fundedAmount floatValue];
// 3) Set the label appropriately
humanReadble.text = [NSString stringWithFormat:@"Latest loan: %@ from %@ needs another $%.2f to pursue their entrepreneurial dream", [loan objectForKey:@"name"],
[(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"],
outstandingAmount];
//build an info object and convert to json
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys: [loan objectForKey:@"name"],
@"who",
[(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"],
@"where",
[NSNumber numberWithFloat: outstandingAmount],
@"what",
nil];
//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error];
//print out the data contents
jsonSummary.text = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];