Android Question Weird number returns from Parser.Nextarray

fsikkerm

Member
Licensed User
Longtime User
Hi All

What am I doing wrong here ??
After using a + sign in front of my data its works fine but without it, i get weird data back ???

In my code retrieve a JSON string

If res.StartsWith ("[{") Then
parser.Initialize(res)
TankData.Clear
TankData=parser.NextArray
' Set data
TmpMap = TankData.Get(0)




Different JSON means Different result, but its a number , why does A: fail ??

A: [{"BatchNo":0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0038276638 ?
B: [{"BatchNo":+0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0222007036 <- Correct

Hope someone knows ..
 

Mahares

Expert
Licensed User
Longtime User
Different JSON means Different result, but its a number , why does A: fail ??
Your code should be something like this:
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim BatchNo As Int = colroot.Get("BatchNo")
Next
Use this tool to parse. It will work
 
Upvote 0

Quandalle

Member
Licensed User

A: [{"BatchNo":0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0038276638

the JSON format comming from javascript left a "blur" on the interpretation of the sequence of digits in number.

A leading zero forces the number to be interpreted as an octal value, (especially since in your case the digits of the number are between 0 and 7, which does not cause a decoding error)
So the octal value 0222007036 corresponds in decimal to 38276638

B: [{"BatchNo":+0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0222007036 <- Correct

When preceded by a + the number is interpreted as being in decimal, so in the case of +0222007036 it gives the right value.

In practice you should avoid having zeros at the head of the number in JSON because it can either cause errors or transcoding depending on the implementation.
JSON doesn't officially support octal numbers, so formally your JSON is invalid. Some parsers do support it though, which may lead to some confusion.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can also have something like this using your variables:
B4X:
 Dim res As String= $"[{"BatchNo":"0222007036"}]"$
    Dim TankData As List
    TankData.Initialize
    If res.StartsWith ("[{") Then
        Dim parser As JSONParser
        parser.Initialize(res)
        TankData=parser.NextArray
        Dim TmpMap As Map
        TmpMap = TankData.Get(0)
        Dim BatchNo As String = TmpMap.Get("BatchNo")
        Log(BatchNo)    'displays: 0222007036        
    End If
 
Upvote 1

emexes

Expert
Licensed User
Or for pulling just a small number of known fields out of JSON, consider using:

B4X:
Dim TankJSON As String = $"Different JSON means Different result, but its a number , why does A: fail ??

A: [{"BatchNo":0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0038276638 ?
B: [{"BatchNo":+0222007036}] then Log( TmpMap.Get("BatchNo")) gives 0222007036 <- Correct :)

Hope someone knows .."$

'I know that regex looks like alien writing if you're not used to it, but it still works even if you can't read it
'just replace "BatchNo" with whatever field name you're looking for
'the \+? will swallow a leading + (if there is one) and the 0* will swallow leading zeroes (if there are any)
'the [0-9\.] means that batch numbers are comprised of digits 0-9 and possibly also dots/full-stops/periods

Dim Pattern As String = "\""" & "BatchNo" & "\""\s*\:\s*\""?\+?0*([0-9\.]*)\""?"

'Log(TankJSON)
'Log(Pattern)

Dim M As Matcher = Regex.Matcher(Pattern, TankJSON)

Do While M.Find
    Log("Batch Number = " & M.Group(1))
Loop

'or if should only have one BatchNo then:
'If M.Find Then
'    Log("Batch Number = " & M.Group(1))
'End If

 
Last edited:
Upvote 0

fsikkerm

Member
Licensed User
Longtime User

Ahh that makes sense..

I have managed to alter the incoming JSON to [{"BatchNo":"0222007036"}] so it works now..

It was an old SQL function that translates XML to JSON ( 'select.... for JSON auto' does not work on the server i have in production )

Again all thx for the great en helpfull input and enjoy your weekend
 
Upvote 0

fsikkerm

Member
Licensed User
Longtime User
Isn't that the answer I gave you in post #4
I do use the same format now [{"BatchNo":"0222007036"}]

The SQL database that returns the JSON is of old version . it as no build in 'For JSON AUTO' so I used two functions
( more info here )
I now changed qfn_JsonEscape scalar function in the database so it recognises a number that should be returned as a string..
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…