我需要实时汇率。我正在使用 URL 中提供的 Google API
http:://www.google.com/ig/calculator?hl=en&q=1USD=?INR
每当使用 json 解析访问该 URL 时,响应数据就会为零。我不明白该代码中的确切错误是什么
#define openexchangeURl @"http://www.google.com/ig/calculator?hl=en&q=1USD=?INR"
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:openexchangeURl]];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
values =[responseString JSONValue];
最佳答案
这将为您提供实时汇率:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://rate-exchange.appspot.com/currency?from=USD&to=INR&q=1"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSDictionary *parsedDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
CGFloat value = [parsedDict[@"rate"] floatValue];
NSLog(@"Value: %f", value);
}];
来自该 API 的 JSON 响应如下所示:
{
to: "INR",
rate: 54.8245614,
from: "USD",
v: 54.8245614
}
您的原始请求没有 NSURLConnection
并且响应不是有效的 JSON(散列中的每个项目没有双引号值)。
关于iphone - -JSONValue 失败。错误跟踪为 : in Google currency converter in iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15082190/