2013-04-12 63 views
0

我在UIAlertView中顯示國際字符時遇到問題。iOS上UIAlertView中的國際字符集

我從this URL檢索JSON格式的字符串。

返回的格式正常,它包含正確的字符。

當我嘗試加載它在我的應用程序在UIAlertView我看到這一點:

enter image description here

什麼是從西班牙,克羅地亞,中國等顯示Unicode字符的正確方法?

更改設置中的默認語言並不能解決問題。

+0

你是如何解碼json的? –

+0

我們正在使用sbjsonparser – mirzahat

+0

你可以發佈你的代碼從JSON獲取字符串並將它們分配給UIAlertView? –

回答

1

沒有任何代碼,很難指出你錯在哪裏。下面的代碼工作,我只能假設你正在做的東西特別與編碼數據的果味。

NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://kancelarijahatipovic.com/translate.php?from=eng&dest=hr&phrase=iron"]] returningResponse:nil error:nil];  
NSString *string = [NSString stringWithUTF8String:[data bytes]]; 
SBJsonParser *parser = [[SBJsonParser alloc] init]; 
id returnVal = [parser objectWithString:string]; 
if([returnVal isKindOfClass:[NSDictionary class]]) { 
    //build string 
    NSMutableString *alertString = [NSMutableString string];  
    if([returnVal objectForKey:@"tuc"] != nil) { 
     NSArray *tucs = [returnVal objectForKey:@"tuc"]; 

     for(id tucObject in tucs) { 
      if([tucObject isKindOfClass:[NSDictionary class]] && [tucObject objectForKey:@"phrase"] != nil) { 
       id phraseObject = [tucObject objectForKey:@"phrase"]; 

       if([phraseObject isKindOfClass:[NSDictionary class]] && [phraseObject objectForKey:@"text"] != nil) { 
        [alertString appendFormat:@"%@\n", [phraseObject objectForKey:@"text"]]; 
       } 
      } 
     } 
    } 

    if(alertString.length > 0) { 
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"example" message:alertString delegate:nil cancelButtonTitle:@"okay" otherButtonTitles: nil]; 
     [alertView show]; 
    } 
} 

編輯:SBJsonParser objectWithData:提供了與上面相同的結果。

+0

這工作,NSString *字符串= [NSString stringWithUTF8String:[數據字節]];做的伎倆,謝謝! – user2274239