Emoji characters cannot be encoded to JSON

后端 未结 6 1836
长发绾君心
长发绾君心 2021-01-31 06:29

I have a UITextView which I call messageField. The data within that messageField is POST-ed to server in JSON format. When th

6条回答
  •  -上瘾入骨i
    2021-01-31 06:58

    Thanks @Karu, editing your response,I use this code:

    Encode for send to server:

    NSString *uniText = [NSString stringWithUTF8String:[text_to_send UTF8String]];
    
    NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    
    NSString *str = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
    

    Decode to receive and showing emoji text:

    NSData *newdata = [received_string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    NSString *mystring = [[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];
    
    NSString *finalString= @"";
    
    if (mystring) {
       finalString = mystring;
    }else{
       finalString = received_string;
    }
    

    This code run perfect on iOS 9

提交回复
热议问题