-JSONValue失败。错误跟踪是:在iPhone中的Google货币转换器中

时间:2013-02-26 05:42:45

标签: iphone ios objective-c

我需要实时汇率。我正在使用网址

中提供的Google API
http:://www.google.com/ig/calculator?hl=en&q=1USD=?INR

每当使用json解析来访问该URL时,响应数据都会变为nil。我没有得到该代码中的确切错误

#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];

1 个答案:

答案 0 :(得分:1)

这将为您提供实时费率:

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(散列中的每个项目都没有双引号值)。