iOS-从后端转换UTF8数据并错误显示
我正在编写一个从后端接收数据的iOS应用程序。我解析NSData对象(objectNotation),如下所示 一切正常,直到我在一个实体中收到一个特殊字符,即: “测试já” 当我收到此消息并调试parsedObject时,它将“type”显示为“Test j\U00e1” 这显然是Unicode/UTF8的问题。然后,我继续查看整个堆栈溢出,找到了各种可能的解决方案,并尝试了所有这些方法,但都没有成功iOS-从后端转换UTF8数据并错误显示,ios,unicode,utf-8,character-encoding,Ios,Unicode,Utf 8,Character Encoding,我正在编写一个从后端接收数据的iOS应用程序。我解析NSData对象(objectNotation),如下所示 一切正常,直到我在一个实体中收到一个特殊字符,即: “测试já” 当我收到此消息并调试parsedObject时,它将“type”显示为“Test j\U00e1” 这显然是Unicode/UTF8的问题。然后,我继续查看整个堆栈溢出,找到了各种可能的解决方案,并尝试了所有这些方法,但都没有成功 NSString *stringTest = [[NSString alloc] init
NSString *stringTest = [[NSString alloc] initWithData:objectNotation
encoding:NSUTF8StringEncoding]; --> does not work
NSString * test2 = [NSString
stringWithCString:[stringTest cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSASCIIStringEncoding]; --> does not work
我已经尝试过Obj提供的每一种编码,它要么返回字符\U00e1,要么返回nil,或者返回一堆垃圾
我已经为此奋斗了几个小时,所以现在是发帖的好时机。我感谢任何帮助。多谢各位
代码如下:
NSDictionary *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
NSMutableArray *btms = [[NSMutableArray alloc] init];
NSArray *results = [parsedObject valueForKey:@"results"];
NSLog(@"Count %d", results.count);
NSLog(@"Count %d", parsedObject.count);
[parsedObject enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
BTM *btm = [[BTM alloc] init];
NSLog(@"%@ => %@", key, value);
btm.number = key;
btm.latitude = [value valueForKey:@"lat"];
btm.longitude = [value valueForKey:@"long"];
NSString* temp = [value valueForKey:@"type”];
}从后端接收数据时,将“application/x-www-form-urlencoded;charset=UTF-8”设置为“内容类型”
[requestUrl setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
嗨,谢谢。我必须将我的NSURLRequest更改为NSMutableURLRequest才能这样做。然后我设置url,然后根据您的代码调用setValue。这是正确的吗?不幸的是,这没有帮助。