Android Question Unexpected behaviour by JSON Parser

FrankBerra

Active Member
Licensed User
Longtime User
Hi everyone!
I am trying to work with HEX and JSON parser. After a lot of troubles i faced a strange thing.
In the following code the User.Get("Surname") returns me the value "4.0" instead of the expected "4d".
("4d" should be the hex rappresentation of the letter "M")

Where i am wrong?

I attach here the working example.

B4X:
Dim Users As String = "[{Name=4d, Surname=4d},{Name=4d, Surname=4d}]"
               
    Dim ListaOfUsers As List
    ListaOfUsers.Initialize
               
    Dim ParserProfili As JSONParser
    ParserProfili.Initialize(Users)
    ListaOfUsers = ParserProfili.NextArray
   
    For Each User As Map In ListaOfUSers
        Log("Name: " & User.Get("Name"))
        Log("Surname: " & User.Get("Surname"))
    Next
 

Attachments

  • HexTest.zip
    8.1 KB · Views: 206

Roycefer

Well-Known Member
Licensed User
Longtime User
"4d" is a valid Double so User.Get("Name") is converting it to a Double (4) and then to a String ("4.0") and then Logging it. You need to use Byte Converter to treat it as a hex String and convert it to a String, like so:

B4X:
Dim bc As ByteConverter
Log("Name: " & bc.StringFromBytes(bc.HexToBytes("Name: " & User.Get("Name")),"UTF8"))
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Thanks, but the problem is still there because User.Get("Name") retrieve the wrong string and so converting it with byteconverter it says:
java.lang.Exception: hex string has odd number of characters
 
Upvote 0

Jorge M A

Well-Known Member
Licensed User
The names and values must be enclosed by double quotes (QUOTE) to be interpreted as strings.
Your string construction doesn't permit this. In your sample code, I use stringbuilder to acomplish this.
See your modified project attached.
 

Attachments

  • ProveEsadecimale_SB.zip
    8.2 KB · Views: 198
Upvote 0
Top