Extracting a value from a HTML file as an NSString
I'm creating a currency converter and using the Google Finance API to do so.
I'm following these simple instructions.
The url outputs a page that look开发者_如何学编程s something like this:
{lhs: "100 Euros",rhs: "132.240437 Australian dollars",error: "",icc: true}
My question is, what is the best way to extract the outputted value (132.240437) as a string?
The simplest way is to use RegEx (e.g. RegexKit Lite). For this task you'll need:
[respStr stringByMatching:@".*rhs: \"([0-9\-\.]*)" capture:1L];
Get a simple JSON parser like this one. Then
#include "JSON.h"
After getting your JSON string called, say, download
from the API:
NSDictionary *currencyData = [download JSONValue];
NSString *numberString = [currencyData objectForKey:@"rhs"];
numberString = [[numberString componentsSeparatedByString:@" "] objectAtIndex:0];
精彩评论