Android Question Problem With Emojis and json_decode

Discussion in 'Android Questions' started by Marcos Alves, May 17, 2018 at 9:36 PM.

  1. Marcos Alves

    Marcos Alves Active Member Licensed User

    Hello...

    I'm getting a problem when using jsonparser fuction as follows:

    - Device A sends data using a POST command to a server, and then the server delivers to device B
    - Suppose that data is an emoji like this: ☺
    - The data is correctly received by the server and stored in mysql database (correctly).
    - When requested by B, the data is codified using json_decode due the presence of other fields for data control, generating something like this:

    {"MESSAGERECEIVED":{"ID":"CC8667C65B900ED106FC8201807FCB07F07C9AF8470B1FA2F48333320D9CC720","IDCHAT":"BB2071692B892610D3222E9044384C27A05FD7FC1EEA30A738B39E2632339EB6","MSGDATA":"\u263a","MSGCREATED":1526588421517,"IDSOURCE":"638"}}

    - Notice that the field "MSGDATA" is the original emoji correctly codified by json_encode at server side...

    To receive data from server, a POST answer in the side B, using jsonparser, runs the routine:

    Code:
    Dim Resposta As Map
                                   Resposta.Initialize
                                   Resposta = Json1.NextObject
                           
                                    
    Dim NewMessage As Map
                                    NewMessage.Initialize
                                 
                                    NewMessage = Resposta.Get(
    "MESSAGERECEIVED")
                                 
                                    
    Dim IdMessage As String = NewMessage.Get("ID")
                                    
    Dim IdChat As String = NewMessage.Get("IDCHAT")
                                    
    Dim MsgData As String = NewMessage.Get("MSGDATA")
                                    
    Dim MsgCreated As Long = NewMessage.Get("MSGCREATED")
                                    
    Dim IdSource As Int = NewMessage.Get("IDSOURCE")
    ...

    - Showing MsgData content in a label.text in B side I got u263a (emoji text code!!!) instead of the emoji pic!

    Then, I'm a little confused here. Shouldn't jsonparser had converted the \u263a to ☺ ? What am I doing wrong ?
     
  2. ronell

    ronell Well-Known Member Licensed User

    take a look at this thread https://www.b4x.com/android/forum/threads/is-there-a-good-solution-for-display-emoji-in.61083/

    then maybe this will work
    Code:
    Dim MsgData As String = NewMessage.Get("MSGDATA")

    Dim Msg as string

    Msg=
    "0x"&MsgData.SubString(1)


    edittext1.text = UTS(Msg)
    Sub UTS (codepoint As Int) As String
      
    Dim bc As ByteConverter
      
    Dim b() As Byte = bc.IntsToBytes(Array As Int(codepoint))
      
    Return BytesToString(b, 04"UTF32")
    End Sub
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Code:
    Dim json As JSONParser
    json.Initialize(
    $"{"MSGDATA":"\u263a"}"$)
    Dim m As Map = json.NextObject
    Activity.Title = m.Get("MSGDATA")
    Result:

    [​IMG]
     
    ronell likes this.
  4. Marcos Alves

    Marcos Alves Active Member Licensed User

    Hi @Erel . Fast answering as always. Thanks!
    I understand that I can decode json data came from server but I found a better way... what if all the flow A->server->B could maintain the original Android format, even after successive json encode/decode? If it can be done, it isn't needed to worry with format conversion. I noticed that there are many different notations for json depending on php version, apache, android, ios and so on.

    The problem was in server side notation when I used json_encode in php... something like this:

    PHP:
    $message["MESSARECEIVED"] = $messagefields;

    echo 
    json_encode($message);
    The solution to maintain recognizable format by Android is to use in server:

    PHP:
    $message["MESSAGERECEIVED"] = $messagefields;

    echo 
    json_encode($message,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
    The options JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES tell to php interpreter to maintain the Android format and then we can storage in mysql using utf8mb4 and send back using echo showing directly in Android without any change!

    I hope that this information could help any other community member that could be having the same issue...
     
    ronell likes this.
  5. Marcos Alves

    Marcos Alves Active Member Licensed User

    Thanks! I found the solution... please, see my post in this thread.
     
Loading...