1

次の方法でサーバーにリクエストを送信しています。

- (void) getData
{
    NSString *url=  @"http://example.com";

    NSMutableURLRequest *theRequest= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                     timeoutInterval:10.0];

exampleConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];

}

そして、このための応答はJSONファイルであり、次のように解析しています。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Connection did finish loading %@",[connection currentRequest]);

    NSDictionary *exampleDictionary = [NSJSONSerialization JSONObjectWithData:receivedData options: NSJSONReadingAllowFragments error:nil];

    NSLog(@"%@", exampleDictionary); // which gives me null

}

応答は以前は正常に機能しました。しかし、今ではnullが返されます。これは、JSONからNSDictionaryへの変換を何かが妨げているためです。調査したところ、問題は、解析プロセスがうまくいかなかった外国人のキャラクター(中国語など)が2人いたことが原因であることがわかりました。

この問題を解決する方法がわかりません。誰かアイデアはありますか?

4

1 に答える 1

1

クラスとエンコーディングオプションをいじって、最終的に次のことを行い、問題を解決しました。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"Connection did finish loading %@",[connection currentRequest]);

    NSString *res = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];

    NSDictionary *exampleDictionary = [NSJSONSerialization JSONObjectWithData:[res dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingAllowFragments error:nil];

    NSLog(@"%@", exampleDictionary); // gives the actual data! :)

}
于 2013-03-19T16:27:55.770 に答える