0

JSONを非同期に取得するためにAFNetworkingを使用しようとしています。

UTF8でエンコードされたjsonファイルがあり、AFJSONRequestOperationがUTF8コードで辞書を返します。

éphémère   -->   \U00c3\U00a9ph\U00c3\U00a9m\U00c3\U00a8re

UTF8をAFJSONRequestOperationでうまく機能させる方法はありますか?

AFJSONRequestOperationを使用する2番目の問題があります:彼は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を使用していましたが、うまく機能します(charset + BOM)!しかし、非同期呼び出しを行う必要があります。

ご協力ありがとうございました!

4

1 に答える 1

1

これはNSLogの問題であり、JSONは有効です。その中のキーの値を取得して、その有効な文字を確認できます。

このコードを使用して、応答JSONを読みやすくします。

-(NSString*)stringByReplacingUnicodePoint:(id)jsonObj
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:jsonObj options:0 error:nil];
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return result;
}

-(void)networkResponse:(id)JSON
{
    NSLog(@"JSON: %@", [self stringByReplacingUnicodePoint: JSON]);
}
于 2014-08-25T11:01:17.150 に答える