Android Question JSON parsing strange error

Tomas Petrus

Active Member
Licensed User
Longtime User
I am somehow trapped...
everything worked fine until I added declaration and parsing of second list...
regardless what I do it will always end with null string error. If i switch order of the 2 lists it always crash on the last one....

First I thought its datetimeparse related. - so I separated it to the SUB. but it didnt help now the error seems that is on last line of the conversion function..but its not..

If i switch the order of the lists the error will be on line
B4X:
If    mHelper2.Get("jazyk") == Jazyk Then

B4X:
Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim m As Map = parser.NextObject
        Dim mHelper As Map
        Dim mHelper2 As Map
   
        Dim error As String = m.Get("error") 'null = bez erroru
        Dim hlaska As String = "error"
        Dim Term1Od As Long 
        Dim Term1Do As Long
        Dim Term2Od As Long
        Dim Term2Do As Long
        Dim Cas = DateTime.Now As Long
        Dim CasOK As Boolean = False
       
        If    error <> Null Then
            'parsovani hlasky a vyber spravneho jazyka
            Dim hlasky As List
            hlasky = m.Get("hlasky")
            For i = 0 To hlasky.Size - 1
                mHelper2 = hlasky.Get(i)
                If    mHelper2.Get("jazyk") == Jazyk Then
                    hlaska = mHelper2.Get("text")
                End If
            Next
           
            'parsovani a naplneni rozvrhu
            Dim casy As List
            casy = m.Get("casy")
            For i = 0 To casy.Size - 1
                mHelper = casy.Get(i)
                Log(mHelper.Get("od"))
                Log(mHelper.Get("do"))
               
                If i == 0 Then
                    Term1OdS = KonverzeCasu(mHelper.Get("od"))
                    Term1DoS = KonverzeCasu(mHelper.Get("do"))
                else If i == 1 Then
                    Term2OdS = KonverzeCasu(mHelper.Get("od"))
                    Term2DoS = KonverzeCasu(mHelper.Get("do"))
                End If
            Next

       End If

Sub KonverzeCasu (cas As String) As Long
    Dim casTick As Long
    Log ("konvert " & cas)
'    cas = "23:59:00"
    casTick = DateTime.TimeParse(cas)
    Log ("konvert" & casTick)
    Return casTick
End Sub

so regardless what I do it always crash ...and the error is not helping in debuging the problem...
I tried to separate helpers but it didnt do the trick neither.
any ideas ?
 

DonManfred

Expert
Licensed User
Longtime User
What is the json you want to Parse?
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
B4X:
{
  "Casy": [{
    "Od": "00:00:00",
    "Do": "09:00:00"
  }, {
    "Od": "19:00:00",
    "Do": "23:59:00"
  }],
  "Error": null,
  "Hlasky": [{
    "Text": "Objednáváte mimo naše standardní provozní hodiny. Jezdíme 00:00 - 9:00 a 15:00 - 23:59",
    "Jazyk": "cs"
  }, {
    "Text": "rozvrh",
    "Jazyk": "sk"
  }, {
    "Text": "rozvrh",
    "Jazyk": "de"
  }, {
    "Text": "rozvrh",
    "Jazyk": "hu"
  }, {
    "Text": "rozvrh",
    "Jazyk": "en"
  }]
}
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
I read it all I can see and write to log any parameters... everything seems to be working... it just crashed at the end...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim Hlasky As List = root.Get("Hlasky")
For Each colHlasky As Map In Hlasky
 Dim Jazyk As String = colHlasky.Get("Jazyk")
 Dim Text As String = colHlasky.Get("Text")
Next
Dim Error As String = root.Get("Error")
Dim Casy As List = root.Get("Casy")
For Each colCasy As Map In Casy
 Dim Od As String = colCasy.Get("Od")
 Dim Do As String = colCasy.Get("Do")
Next

everything seems to be working... it just crashed at the end...
With which error?
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
B4X:
{"casy":[{"od":"00:00:00","do":"09:00:00"},{"od":"19:00:00","do":"23:59:00"}],"error":null,"hlasky":[{"text":"Objednáváte mimo naše standardní provozní hodiny. Jezdíme 00:00 - 9:00 a 15:00 - 23:59","jazyk":"cs"},{"text":"rozvrh","jazyk":"sk"},{"text":"rozvrh","jazyk":"de"},{"text":"rozvrh","jazyk":"hu"},{"text":"rozvrh","jazyk":"en"}]}
00:00:00
09:00:00
konvert 00:00:00
konvert1542582000000
konvert 09:00:00
konvert1542614400000
19:00:00
23:59:00
konvert 19:00:00
konvert1542650400000
konvert 23:59:00
konvert1542668340000
Error occurred on line: 923 (Main)
java.lang.NumberFormatException: For input string: "null"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:539)
    at anywheresoftware.b4a.debug.RDebugUtils.numberCast(RDebugUtils.java:55)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA$2.run(BA.java:365)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

the error is on different lines when I switch the lists but its the same null thing
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
atm line 923 is Return casTick from convert function.

but the last input is correctly converted...
B4X:
konvert 23:59:00
konvert1542668340000
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
If i switch the order of the lists the error will be on line
If mHelper2.Get("jazyk") == Jazyk Then
Sorry if this is a dumb question but is it permissable to use "==" in place of "=" in B4X?
 
Upvote 0

Tomas Petrus

Active Member
Licensed User
Longtime User
I don't know I got a lot of C# and JS habits..and sometimes I forget what is what : )
So for me when it is not red and its working, I dont question it : ))
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
I don't know I got a lot of C# and JS habits..and sometimes I forget what is what : )
So for me when it is not red and its working, I dont question it : ))
Something new every day! :D
 
Upvote 0

udg

Expert
Licensed User
Longtime User
is it permissable to use "==" in place of "=" in B4X?
Short answer: yes. It is the comparision operator (while "=" should be the assignment operator).
But, the B4x compiler is smart enough to let us use almost always the single equal operator.
In most cases it's a readability help for yourself

A couple of examples:
B4X:
dim i as int = 7
if i = nexInt(i) then.. else ..
NextInt returns the next integer greater than i, (8 in the example above). The compiler understands it's a comparision, returning always False.

B4X:
dim switch as boolean = false
if switch = ReverseSwitch(switch) then.. else
We could read the above like if (False = True) leading to the else branch or we could read it as "assign the result of function ReverseSwitch to variable switch", in which case we will have switch=True (assignment) and so the If should evaluate True taking the Then branch.
I guess that in this specific case we should help the compiler enclosing (or not enclosing) the if test in paratheses or using the double equal sign.
I didn't test it, but it feels logical.

Or, even simpler, the compiler could assume that an equal sign in the test part of the if-then-else is always a comparision.

Update: a quick test seems to show that the compiler assumes an equal sign to be a comparision if found in the if test section.
 
Last edited:
Upvote 0
Top