【发布时间】:2012-12-24 17:54:30
【问题描述】:
我正在尝试使用 AFNetworking 异步获取 JSON。
我有一个 UTF8 编码的 json 文件,AFJSONRequestOperation 用 UTF8 代码返回我的字典:
éphémère --> \U00c3\U00a9ph\U00c3\U00a9m\U00c3\U00a8re
有没有办法让 UTF8 与 AFJSONRequestOperation 一起工作?
使用 AFJSONRequestOperation 存在第二个问题:他无法使用 BOM 读取 UTF8 文件。
这是我的代码:
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
NSLog(@"JSON: %@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
在此之前,我使用的是 JSONKit,而且效果很好(字符集 + BOM)!但我需要进行异步调用。
感谢您的帮助!
【问题讨论】:
-
\U是无效的 JSON。请参阅json.org 的语法 -
我的 JSON 是 {"test" : "éphémère"}。 NSLog 显示如下: JSON: { test = "\U00c3\U00a9ph\U00c3\U00a9m\U00c3\U00a8re"; }
标签: json utf-8 character-encoding afnetworking