NSJSONSerialization and Emoji

I am currently trying to POST some JSON containing emojis in the python API. I tried to serve NSJSONSerialization directly using a string containing emojis from my UITextField, but the serializer crashed without any meaningful explanation. Subsequently, I tried to convert the format and got the following:

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

This basically works, except that the resulting UTF-8 is kind of double “shielded”, which leads to the following:

"title":"\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d\\udc8f\\ud83d"

Any suggestions for fixing this?

+3
source share
2 answers

:
 1. Apple hosed NSString WRT UTF Planes 1 ,   UTF-16. , length 2    .
 2. , emoji Plane 1,    , 1    UTF .

( @Hot Licks): OP emoji

NSString *uniText = @"💦💏👒👒💦";
NSDictionary* jsonDict = @{@"title":uniText};

NSData * utf32Data = [uniText dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
NSLog(@"utf32Data: %@", utf32Data);

NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0 error:&error];
if (jsonData == nil) {
    NSLog(@"JSON serialization error: %@", error);
}
else {
    NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"The JSON result is %@", jsonString);
    NSLog(@"jsonData: %@", jsonData);
}

NSLog

utf32Data: a6f40100 8ff40100 52f40100 52f40100 a6f40100
JSON: { "title": "💦💏👒👒💦" }
jsonData: 7b227469 746c6522 3a22f09f 92a6f09f 928ff09f 9192f09f 9192f09f 92a6227d

+4

:

NSString* uniText = mytextField.text;
NSDictionary* jsonDict = @{@"title":uniText};
NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJsonObject:jsonDict options:0 error:&error];
if (jsonData == nil) {
    NSLog(@"JSON serialization error: %@", error);
}
else {
    NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"The JSON result is %@", jsonString);
}

myTextField.text NSString, . NSJSONSerialization "".

0

All Articles