Ios (Obj-C)从NSHTTPPURLRESPONSE(allHeaderFields)用字典中的重音符号解码JSON
我通过以下方式从NSHTTPPURLResponse接收头字段作为NSDictionary:Ios (Obj-C)从NSHTTPPURLRESPONSE(allHeaderFields)用字典中的重音符号解码JSON,ios,objective-c,json,dictionary,Ios,Objective C,Json,Dictionary,我通过以下方式从NSHTTPPURLResponse接收头字段作为NSDictionary: NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]); NSDictionary *rootDictionary = [response allHeaderFields]; NSError * err; NSData * jsonData = [NSJSONSerialization dataWithJSONObje
NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
NSDictionary *rootDictionary = [response allHeaderFields];
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:[response allHeaderFields] options:0 error:&err];
id rootObject = [NSJSONSerialization
JSONObjectWithData:jsonData
options:0
error:&error];
NSDictionary *rootDictionary = rootObject;
但在本词典的值中有重音符号,当我试图显示为Alert时,会出现符号,而不是带有重音的字母。我用这种方式:
NSHTTPURLResponse *response = ((NSHTTPURLResponse *)[task response]);
NSDictionary *rootDictionary = [response allHeaderFields];
NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:[response allHeaderFields] options:0 error:&err];
id rootObject = [NSJSONSerialization
JSONObjectWithData:jsonData
options:0
error:&error];
NSDictionary *rootDictionary = rootObject;
在我的标题中,我收到以下信息:
headers {
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Language" = en;
"Content-Length" = 30;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Thu, 10 Dec 2015 15:47:07 GMT";
Expires = "-1";
"Keep-Alive" = "timeout=5, max=100";
Pragma = "no-cache";
Server = "xxxxxxxxxx";
"X-AspNet-Version" = "xxxxx";
"X-Error" = "Par\U00c3\U00a1metros faltantes";
"X-Powered-By" = "xxxxxx";
}
我在警报中显示X错误值:
NSString *description = [rootDictionary objectForKey:@"X-Error"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title of my alert" message:description delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil];
[alert show];
这是我的结果:
法尔坦茨地铁站
这是我在Xcode(NSLog)中的字典日志:
有其他解决方案吗?回答:
我可以这样做:
NSDictionary *headerField = [response allHeaderFields];
NSString *description = [headerField valueForKey:@"X-Error"];
NSString *correctString = [NSString stringWithCString:[description cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
重要元素是cstringusingencode:nsisolatin1stringecoding。给我正确的口音 您需要更新您的问题,并提供有关您拥有的数据和看到的结果的更多详细信息。Ready@rmaddy,我使用
rootDictionary
的NSLog
输出更新您的问题,并显示用于从rootDictionary
获取警报的代码。