我想通过使用Json解析发送一个字符串到服务器。我的字符串包含许多特殊字符,如ó,æ,ø,å等等。当我发送数据到服务器没有任何特殊的字符时,它工作得很好,响应如预期。但是,如果甚至有一个特殊的字符,那么它在解析数据时会显示错误。 我使用下面的代码来解析JSON字符串: -在iOS中解析Json时的特殊字符。
NSURL *theURL = [NSURL URLWithString:urlToSendRequest];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
NSData *requestData = [NSData dataWithBytes:[jsonContentToSend UTF8String] length:[jsonContentToSend length]];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody: requestData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *data=[[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSLog(@"url to send request= %@",urlToSendRequest);
NSLog(@"jsoncontenttosend= %@",requestData);
NSLog(@"response1111:-%@",data);
return data;
这里urlToSendRequest是URL和jsonContentToSend是我发送到服务器的特殊字符字符串。
谢谢马丁。非常感谢。这解决了我的问题。现在我正在得到所需的答复。 – 2013-05-14 06:22:08