2013-02-26 41 views
0

谷歌貨幣轉換器我需要實時貨幣匯率。我正在使用一個谷歌API在URL中可用-JSON值失敗。錯誤跟蹤是:在iPhone

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]; 
+0

第二?在查詢字符串中應該是一個紅旗。仔細檢查您的網址。 – Jeremy 2013-02-26 05:45:19

+0

可以請示例代碼如何解析網址,也是{lhs:「1美元」,rhs:「54.0336089印度盧比」,錯誤:「」,icc:true}這個數據後,該網址我只需要值是54.0336089 – areddy 2013-02-26 05:47:50

+0

聽起來像一個不同的問題。你是否說你不再得到零響應? – Jeremy 2013-02-26 05:49:44

回答

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(哈希中的每個項目都沒有雙引號值)。

+0

謝謝你遲到和尼克答案... 。 – areddy 2013-03-02 05:24:23