2010-04-26 5 views
4

안녕 얘들 아, JSON 배열을 잡고 NSArray에 저장하고 있지만 JSON 인코딩 된 UTF-8 문자열이 포함되어 있습니다 (예 : pass \ u00e9는 passé를 나타냄). 이러한 모든 유형의 문자열을 실제 문자로 변환하는 방법이 필요합니다. 변환 할 전체 NSArray가 있습니다. 또는 내가 가장 쉽게 볼 수있을 때 변환 할 수 있습니다.UTF-8 변환

나는이 차트 http://tntluoma.com/sidebars/codes/

이 편리한 메소드 또는 내가 다운로드 할 수있는 라이브러리가 발견?

덕분에,

은 BTW, 당신은 NSScanner에 따라 접근 방식을 사용할 수 있습니다 나는 단지 내 말에 그것을 해결할 수 있도록 서버를 변경 찾을 수있는 방법 ...

+0

클래스 메소드'+ (id) stringWithUTF8String : (const char *) bytes'를 사용하여'NSString' 객체를 생성하면 올바르게 표시됩니까? – catsby

+0

예제를 제공 할 수 있습니까? 나는 const char 사용에 익숙하지 않다. 감사 – leachianus

답변

1

없다. 다음 코드 (안 버그 증거) 할 수있는 당신에게 그것이 작동하는 방법에 대한 방법 제공 : 당신이 JSON Framework를 사용하는 경우

NSString *source = [NSString stringWithString:@"Le pass\\u00e9 compos\\u00e9 a \\u00e9t\\u00e9 d\\u00e9compos\\u00e9."]; 
NSLog(@"source=%@", source); 

NSMutableString *result = [[NSMutableString alloc] init]; 
NSScanner *scanner = [NSScanner scannerWithString:source]; 
[scanner setCharactersToBeSkipped:nil]; 
while (![scanner isAtEnd]) { 
    NSString *chunk; 

    // Scan up to the Unicode marker 
    [scanner scanUpToString:@"\\u" intoString:&chunk]; 
    // Append the chunk read 
    [result appendString:chunk]; 

    // Skip the Unicode marker 
    if ([scanner scanString:@"\\u" intoString:nil]) { 

     // Read the Unicode value (assume they are hexa and four) 
     unsigned int value; 
     NSRange range = NSMakeRange([scanner scanLocation], 4); 
     NSString *code = [source substringWithRange:range]; 
     [[NSScanner scannerWithString:code] scanHexInt:&value]; 
     unichar c = (unichar) value; 

     // Append the character 
     [result appendFormat:@"%C", c]; 

     // Move the scanner past the Unicode value 
     [scanner scanString:code intoString:nil]; 
    } 
} 

NSLog(@"result=%@", result); 
+0

유망 보이는, 그러나 이미이 일을하고 – leachianus

1

을, 당신이 할 모든 당신의 JSON 문자열을 얻을 그래서 같은 NSArray로 변환입니다 :

NSString * aJSONString = ...; 
NSArray * array = [aJSONString JSONValue]; 

라이브러리는 잘 작성되어 있으며 UTF8 인코딩을 자동으로 처리하므로이 외의 작업은 필요하지 않습니다. 나는 상점에있는 앱에서이 라이브러리를 여러 번 사용했습니다. I 매우이 방법을 사용하는 것이 좋습니다.

+0

"셀 '의 핵에"예를 들어, 모든 종류의 문자를 변환하는 방법을 나는 보지 않는다 : \t NSURL * URL = [NSURL URLWithString : 문자열] ; \t NSString * jsonreturn = [[NSString alloc] initWithContentsOfURL : url]; \t \t NSDictionary * dict = [jsonreturn JSONValue]; – leachianus