Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在iOS中解析Json时使用特殊字符。_Iphone_Json_Utf 8 - Fatal编程技术网

Iphone 在iOS中解析Json时使用特殊字符。

Iphone 在iOS中解析Json时使用特殊字符。,iphone,json,utf-8,Iphone,Json,Utf 8,我试图通过使用Json解析向服务器发送一个字符串。我的字符串包含许多特殊字符,如ó、æ、ø、å等。当我在不使用任何特殊字符的情况下将数据发送到服务器时,它工作正常,响应如预期。但是,如果只有一个特殊字符,则在解析数据时会显示错误。 我使用以下代码来解析Json字符串:- NSURL *theURL = [NSURL URLWithString:urlToSendRequest]; NSMutableURLRequest *theRequest = [NSMutableURLRequest re

我试图通过使用Json解析向服务器发送一个字符串。我的字符串包含许多特殊字符,如ó、æ、ø、å等。当我在不使用任何特殊字符的情况下将数据发送到服务器时,它工作正常,响应如预期。但是,如果只有一个特殊字符,则在解析数据时会显示错误。 我使用以下代码来解析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是我用特殊字符发送到服务器的字符串。

处理这种情况的一个好方法是将值作为base64编码值发送,并在将其从JSON解析为类似字符串的数据结构之后,可以解码Base64字符串,并且可以在不使用实际JSON的情况下返回特殊字符


祝你好运

在中创建POST数据时出错

NSData *requestData = [NSData dataWithBytes:[jsonContentToSend UTF8String] length:[jsonContentToSend length]];
[jsonContentToSend length]
是Unicode字符数,而不是数据中的字节数 UTF-8字符串。如果
jsonContentToSend
包含非ASCII字符,则
requestData
将太短,只包含JSON请求的截断部分

您应该将这一行替换为

NSData *requestData = [jsonContentToSend dataUsingEncoding:NSUTF8StringEncoding];

但是我已经在使用NSUTF8StringEncoding了。NSUTF8StringEncoding不能处理特殊字符串吗?base64和utf8之间存在显著差异encoding@samfisher是的,我同意base64和utf8编码之间存在显著差异,但NSUTF8StringEncoding足够强大,可以处理所有这些。谢谢你的回复。谢谢马丁。谢谢。这解决了我的问题。现在我得到了想要的回应。