2017-02-07 2 views
0

und ich möchte Werte daraus erhalten.NSJSONSerialization funktioniert nicht mit Emoji

String

"[{\" VisitorId \ "\" dbd1c1d6-8348-4674-9e48-65137bdc5c14 \“\ " OperatorID \": null, \ "VisitorSignalId \": \ "6c454fc2-cf74-4010-aecd-93d93408ad24 \", "Type \": \ "Visitor \", \ "Messagetext \": \ "ksksksksksk hat sich dem Chat angeschlossen! \", \ "IsJoin \": true, "MessageTime \": \ "2017-02-07 05:10:28 \", \ "File \": null, \ "MessageType \": \ "JoinMessage \", \ "Name \": "ksksksksk", "Email": "[email protected]", "IP": "123.231.110.188", "Seite": "http://www.sathyabaman.com/", " Bewertung \ ": \" Keine Bewertung \ ", \" Bild \ ": null," CurrentOpImage ": null," FileUploadDetails ": null," FirstOperatorName ": null," OperatorCo unt \ ": 1, \" RequestTranscript \ ": false, \" Country \ ": null, \" OS \ ": null, \" Broswear \ ": null, \" Mobile \ ": null," iso_code \ " ": null}, {\" VisitorId \ ": \" dbd1c1d6-8348-4674-9e48-65137bdc5c14 \ ", \" OperatorID \ ": null, \" VisitorSignalId \ ": null," Type \ ": \" Visitor \ ", \" Messagetext \ ": \" lkkhnl \ ", \" IsJoin \ ": false, \" MessageTime \ ": \" 2017-02-07 05:10:58 \ ", \" File \ ": null, \" MessageType \ ": \" NormalMessage \ ", \" Name \ ": \" ksksksksk \ ", \" Email \ ": \" [email protected] \ ", \" IP \ ": "123.231.110.188", "Seite": "http://www.sathyabaman.com/", "Bewertung": "Keine Bewertung", "Bild": null, "CurrentOpImage": null, "FileUploadDetails": null, "FirstOperatorName": null, "OperatorCount": 1, "RequestTranscript": false, "Country": null, "OS": null, "Broswer" \ ": null," Mobile ": null," iso_code ": null}, {\" VisitorId \ ": null," OperatorID ":" 81aebba9-ddc3-4f58-b1af-ac912d23a41e " , \ "VisitorSignalId \": null, \ "Type \": \ "Operator \", \ "Messagetext \": \ "wie kann ich Ihnen heute helfen? \" \ ", \" IsJoin \ ": false, \" Botschaft Time \ ": \" 2017-02-07 10:41:11 \ ", \" File \ ": null, \" MessageType \ ": \" NormalMessage \ ", \" Name \ ": \" sathya \ ", \" Email \ ": null, \" IP \ ": null, \" Seite \ ": null, \" Bewertung \ ": null, \" Bild \ ": null, \" AktuellesOpImage \ ": null, "FileUploadDetails": null, "FirstOperatorName": "sathya", "OperatorCount": 1, "RequestTranscript": null, "Country": null, "OS": null, "Broswer": null, "Mobile": null, "iso_code": null}, {\ "VisitorId \": \ "dbd1c1d6-8348-4674-9e48-65137bdc5c14 \", \ " OperatorID \ ": null, \" VisitorSignalId \ ": \" 6c454fc2-cf74-4010-aecd-93d93408ad24 \ ", \" Type \ ": \" Visitor \ ", \" Messagetext \ ": \" Besucher ist nein länger aktiv \ ", \" IsJoin \ ": falsch, \" MessageTime \ ": \" 2/7/2017 5:11:53 AM \ ", \" Datei \ ": null," MessageType \ " : \ "TransferMessage \", \ "Name \": \ "ksksksksk \", \ "Email \": \ "[email protected] \", \ "IP \": \ "123.231.110.188 \", \ "Seite": "http://www.sathyabaman.com/", "Bewertung": "Keine Bewertung", "Bild": null, "CurrentOpIm age \ ": null, \" FileUploadDetails \ ": null, \" FirstOperatorName \ ": null, \" OperatorCount \ ": 1, \" RequestTranscript \ ": false, \" Country \ ": null, \" OS \ ": null," Broswer ": null," Mobile ": null," iso_code ": null}, {\" VisitorId \ ": null," OperatorID ":" 81aebba9-ddc3- 4f58-b1af-ac912d23a41e "," VisitorSignalId ": null," Type ":" Operator "," Messagetext ":" Hi "," IsJoin ": false," MessageTime \ ": \" 2017-02-07 10:47:54 \ ", \" Datei \ ": null, \" MessageType \ ": \" NormalMessage \ ", \" Name \ ": \" sathya \ ", \" Email \ ": null, \" IP \ ": null, \" Seite \ ": null, \" Bewertung \ ": null, \" Bild \ ": null, \" AktuellesOpImage \ ": null, "FileUploadDetails": null, "FirstOperatorName": "sathya", "OperatorCount": 1, "RequestTranscript": null, "Country": null, "OS": null, "Broswer": null, "Mobile": null, "iso_code": null}, {\ "VisitorId \": null, "OperatorID": "81aebba9-ddc3-4f58- b1af-ac912d23a41e \ ", \ VisitorSignalId \": null, \ "Type \": \ "Operator \", \ "Messagetext \": \ "Wie kann ich Ihnen heute helfen?\ ", \" IsJoin \ ": false, \" MessageTime \ ": \" 2017-02-07 10:50:07 \ ", \" Datei \ ": null," MessageType \ ": \" NormalMessage \ ", \" Name \ ": \" sathya \ ", \" Email \ ": null, \" IP \ ": null, \" Seite \ ": null, \" Bewertung \ ": null, \" Bild \ ": null, \" CurrentOpImage \ ": null, \" FileUploadDetails \ ": null, \" FirstOperatorName \ ": \" sathya \ ", \" OperatorCount \ ": 1, \" RequestTranscript \ ": null, \ "Land": null, "OS": null, "Brower": null, "Mobile": null, "iso_code": null}, {\ "VisitorId \": null, \ "OperatorID": "81aebba9-ddc3-4f58-b1af-ac912d23a41e", "VisitorSignalId": null, "Type": "Operator", "Messagetext": \ "\", \ "IsJoin \": false, "MessageTime \": \ "2017-02-07 10:57:13 \", \ "File \": null, \ "MessageType \": \ "NormalMessage \", \ "Name \": \ "sathya \", \ "Email \": null, \ "IP \": null, \ "Seite \": null, \ "Bewertung \": null, \ "Image \": null, "CurrentOpImage": null, "FileUploadDetails": null, "FirstOperatorName": "sathya", "OperatorCount": 1, "RequestTranscript": null, "Country \" ": null," OS ": null," Brower ": null," Mobi le \ ": null, \" iso_code \ ": null}]"

Methode

+ (void) showCurrentHistory:(NSString *) historyString { 

    NSString* processed = [historyString stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""]; 

    NSString* withotExtras = [[processed substringFromIndex:1] substringToIndex:[processed length] - 2]; 
    NSData *data = [[NSData alloc] initWithBytes:[withotExtras UTF8String] length:withotExtras.length]; 
    NSError *error; 

    NSMutableArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 

    for (NSDictionary *dic in jsonArray) { 
     NSString *VisitorId   = [dic objectForKey:@"VisitorId"]; 
     NSString *VisitorSignalId = [dic objectForKey:@"VisitorSignalId"]; 
     NSString *Type    = [dic objectForKey:@"Type"]; 
     NSString *Messagetext  = [dic objectForKey:@"Messagetext"]; 
     NSString *MessageType  = [dic objectForKey:@"MessageType"]; 
     NSString *MessageTime  = [dic objectForKey:@"MessageTime"]; 
     NSString *Image    = [dic objectForKey:@"Image"]; 
     NSString *FileUploadDetails = [dic objectForKey:@"FileUploadDetails"]; 
     NSString *FirstOperatorName = [dic objectForKey:@"FirstOperatorName"]; 
     NSString *Rating   = [dic objectForKey:@"Rating"]; 

     NSLog(@"----------------------- Current Chat History --------------------------------"); 
     NSLog(@"VisitorId   : %@" , VisitorId); 
     NSLog(@"VisitorSignalId  : %@" , VisitorSignalId); 
     NSLog(@"Type    : %@" , Type); 
     NSLog(@"Messagetext   : %@" , Messagetext); 
     NSLog(@"MessageType   : %@" , MessageType); 
     NSLog(@"MessageTime   : %@" , MessageTime); 
     NSLog(@"Image    : %@" , Image); 
     NSLog(@"FileUploadDetails : %@" , FileUploadDetails); 
     NSLog(@"FirstOperatorName : %@" , FirstOperatorName); 
     NSLog(@"Rating    : %@" , Rating); 
     NSLog(@"------------------------------------------------------------------------------"); 

     MessageItemObject *item = [[MessageItemObject alloc] init]; 
     [item setVisitorId:VisitorId]; 
     [item setVisitorSignalId:VisitorSignalId]; 
     [item setType:Type]; 
     [item setMessagetext:Messagetext]; 
     [item setMessageType:MessageType]; 
     [item setMessageTime:MessageTime]; 
     [item setImage:Image]; 
     [item setFileUploadDetails:FileUploadDetails]; 
     [item setFirstOperatorName:FirstOperatorName]; 
     [item setRating:Rating]; 

     if (messagesList.count == 0) { 
      messagesList = [[NSMutableArray alloc] init]; 
      [messagesList addObject:item]; 

      NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
      [dic setValue:@"Adding new chatting" forKey:@"Adding new Chatting"]; 

      [[NSNotificationCenter defaultCenter] 
      postNotificationName:@"PassData" 
      object:nil 
      userInfo:dic]; 
     } else { 
      [messagesList addObject:item]; 

      NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
      [dic setValue:@"Adding new chatting" forKey:@"Adding new Chatting"]; 

      [[NSNotificationCenter defaultCenter] 
      postNotificationName:@"PassData" 
      object:nil 
      userInfo:dic]; 
     } 
    } 
} 

Diese Methode funktioniert gut, wenn es keine Emoji ist, da dieser Stachel ein einzelnes \ Emoji hat , NSJSONSerialization funktioniert nicht. Kann mir jemand helfen, dieses Problem zu beheben?

+0

Haben Sie das überprüft? http://stackoverflow.com/questions/23814394/nsjsonserialization-and-emoji – User511

+0

Sie müssen das "Two Hear Emoji" durch leere Zeichenfolge ersetzen, wenn Sie es nicht verwenden werden. ODER ersetze es durch seinen Unicode. – Wolverine

+0

@ User511, die TNX funktionierte. –

Antwort

1

Das Problem besteht darin, dass Sie die JSON-Zeichenfolge fest codieren und Änderungen daran vornehmen. Nehmen Sie keine Änderungen an der Zeichenfolge JSON vor und konvertieren Sie sie direkt in NSData.

NSData *data = [historyString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error; 
NSMutableArray* jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 
NSLog(@"Array - %@", jsonArray); 
1

Versuchen Sie es.

Convert String mit Emojis String mit Unicode wie unter

NSData *data = [strEmoji dataUsingEncoding:NSNonLossyASCIIStringEncoding]; 
NSString *unicodeString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

Und wenn Sie irgendwo, dass emoji zeigen wollen, dann können Sie konvertieren zurück in Emoji.

NSData *data = [unicodeString dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *emojiString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];