How to display al the contents of a method using JSONValue?
The following code gives the result of the methods which I am calling individually.
My actual requirement is that I want to call just a particular method, for example a Loan
method, and it should display all the contents in this method.
So what changes should I make in the code below?
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];开发者_Go百科
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:@"loans"];
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
//set the text to the label
label.numberOfLines = 0;
label.text = [NSString stringWithFormat:@"Latest loan: %@ \n \n country: %@ \n \n amount $%.2f", name,country,outstandingAmout ];
}
I am not sure if I understand you right but if you want to just use a method such as
NSString * loanString = [self stringFromLoan:[latestLoans objectAtIndex:0]];
Do this:
-(NSString*)stringFromLoan:(NSDictionary*)loan{
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
return [NSString stringWithFormat:@"Latest loan: %@ \n \n country: %@ \n \n amount $%.2f", name,country,outstandingAmout];
}
So your code would look like this:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:@"loans"];
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
label.numberOfLines = 0;
label.text = [self stringFromLoan:loan];
}
PS.
Don't release the connection it is autoreleased.
UITableviewController can be used for displaying the contents of the NSArray object.
精彩评论