开发者

how to fetch a method from a json file and display it on a Label or webview

开发者 https://www.devze.com 2023-03-14 16:31 出处:网络
i am new to iphone development.i am doing one project where i am connecting to a webservice and getting a json responsestring and parsing json getting the requried output..

i am new to iphone development.i am doing one project where i am connecting to a webservice and getting a json responsestring and parsing json getting the requried output..

when i click a button the required data is getting displayed on label..

//REQUIREMENT

what is my requirement is i shd have a TextField,button and a label or webview in UI

when i enter a string in textfield it should search from the jsonfile and should display the result of particular method on label or webview.

so how to implement this plz suggest changes need to be done in the code.

//source code

.h file

      IBOutlet UILabel* label;

        NSMutableData *dataWebService; 
    }

@property (retain, nonatomic) NSMutableData *dataWebService;

-(IBAction)loadData;

@end

.m file

- (void)loadData

{

    dataWebService = [[NSMutableData data] retain];

    NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]retain];  

      [[NSURLConnection alloc]initWithRequest:request delegate:self];

}






-(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,outstandingAmount];


}    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号