2011-06-21 11 views
0

次のコードは、私が個別に呼び出しているメソッドの結果を示しています。JSONValueを使用してメソッドの内容を表示するにはどうすればよいですか?

実際には、特定のメソッド(たとえば、Loanメソッド)を呼び出す必要があり、このメソッドのすべての内容を表示する必要があります。

私は以下のコードでどのような変更を行う必要がありますか?

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

答えて

0

私は右のあなたを理解してかどうかわからないですが、あなただけのメソッドを使用したい場合など

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

だからあなたのコードは次のようになります。

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

自動リリースされた接続を解放しないでください。

+0

ありがとうございました。私の質問は、メソッドの文字列ローンを書くことなくだった...ちょうどメソッド "ローン"を呼び出すと私たちは "ローンメソッド"に存在するすべてのデータを取得することはできません私は、レスポンスストレスから「ローンメソッド」のコンテンツを取得する – Abhilash

0

UITableviewControllerを使用して、NSArrayオブジェクトの内容を表示できます。

+0

コード内の変更がわかりましたか?私はshdのUIでテーブルビューをドラッグし、コードではウォッチを変更しましたか? – Abhilash

関連する問題