我想通過使用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