Emoji characters cannot be encoded to JSON

后端 未结 6 1843
长发绾君心
长发绾君心 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条回答
  •  醉酒成梦
    2021-01-31 06:46

    I use the below code to encode emoji character

    NSString *uniText = [NSString stringWithUTF8String:[textview.text UTF8String]]; 
    NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding]; 
    NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding] ;
    

    And the below code to decode and display in UILabel

    const char *jsonString = [body UTF8String]; 
    NSData *jsonData = [NSData dataWithBytes:jsonString length:strlen(jsonString)]; 
    NSString *goodMsg = [[NSString alloc] initWithData:jsonData encoding:NSNonLossyASCIIStringEncoding];
    

提交回复
热议问题