如何使用JSONValue显示方法的内容? [英] How to display al the contents of a method using JSONValue?

查看:46
本文介绍了如何使用JSONValue显示方法的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码给出了我分别调用的方法的结果.

The following code gives the result of the methods which I am calling individually.

我的实际要求是我只想调用一个特定的方法,例如 Loan 方法,并且它应该显示此方法中的所有内容.

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];  

    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]];

执行此操作:

-(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.

这篇关于如何使用JSONValue显示方法的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆