Can we retrieve a particular method from json file and display it on UI ?
Hi everyone, I am new to iphone development..I am doing some application where I am stuck...I am having a textfield, button and a webview..
I am having a parsed json file..
I want to enter a string or method in the textfield, which is present in the JSON file..It should match with the method in json file and should display the contents of a method on webview..
I have done with button and a webview... I want to extend further with textfield and webview..
How can we do it? Please suggest me with example code...
//code
- (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 re开发者_StackOverflowlease];
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];
}
I think you are asking this: "How can I enter part of the URL in a textfield, and have it appended to a base URL and loaded into the webview?"
If so, you can do it quite easily. If your textfield is called "textfield" and webview called "webview" then do this:
NSString *baseUrl = @"http://whatever.com";
NSString *fullUrl = [baseUrl stringByAppendingString:textfield.text];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:fullUrl]]];
Trigger it either with a button press or with UITextFieldDelegate's - (BOOL)textFieldShouldReturn:(UITextField *)textField
Edit:
You want to retrieve a value from the NSDictionary that the JSON generated based on a key you specify in the textfield. First, do
NSString *value = [dictionary objectForKey:textfield.text];
to get the value (assuming it is a string here). Then you want to display the value in a webview. You can either do it with loadHTMLString:baseURL:
or with javascript via stringByEvaluatingJavaScriptFromString:
(both methods of UIWebView). If you want to use loadHTMLString for example you can put in
NSString *html = [NSString stringWithFormat:@"<html><body>%@</body></html>", value];
[webview loadHTMLString:html baseURL:nil];
精彩评论