iphone - -JSONValue 失败。错误跟踪为 : in Google currency converter in iPhone

标签 iphone ios objective-c

我需要实时汇率。我正在使用 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/

相关文章:

ios - 通过 Storyboard使用自动布局创建拉伸(stretch)标题

iphone - 您知道哪些 iPhone OS 内存管理规则和操作方法?

ios - 为什么CALayer的动态@property可以调用setter方法?

iphone - 在应用程序中使用 iPhone 默认警报声音

iphone - Objective-c IN APP PURCHASE 实现时出现错误

iphone - NSMutableArray 不获取 XML 值

ios - Swift:使用 collectionView 创建 imageView 堆栈

iphone - 如何转换JSON查询?

ios - 当我使用 .ascii 编码解码 HTML 页面时出现奇怪的字符串

objective-c - 隐藏 KeyWindow 以截取空白屏幕截图