Android Question json read error

jchal

Active Member
Licensed User
Longtime User
hi all
i tryed to read this json
{"draw":{"drawTime":"21-07-2017T10:00:00","drawNo":626098,"results":[65,72,45,27,80,3,1,17,20,52,79,56,30,50,69,8,43,22,23,32]}}

but i got the error below
Error occurred on line: 81 (ActK)
java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
at com.kino.myapp.actkino._jobdone(actkino.java:494)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$2.run(BA.java:338)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
** Service (starter) Destroy **
** Activity (actk) Pause, UserClosed = true **

the code i used in order toread this is
B4X:
Sub LoadKdraw
    Dim jobadd As HttpJob
    jobadd.Initialize("kdraw", Me)
    jobadd.Download("http://mydomainname.com/last.json")
    ProgressDialogShow("Downloading kdraw")
End Sub


Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "kdraw" Then
            Dim quizlist As List
            'Dim strOnline As String
            quizlist = parser.NextArray 'returns a list with maps
            For i = 0 To quizlist.Size - 1
                Dim m As Map
                m = quizlist.Get(i)
                Dim TL As quizpar 'TwoLines
              
                TL.First = m.Get("kdraw")
                TL.Second = m.Get("kresult1")
                TL.third=m.Get ("kresult2")
                TL.forth=m.Get ("kresult3")
                TL.five=m.Get("kresult14")
                TL.six=m.Get("kresult15")
              
                Label1.Text= TL.First
                Label2.Text=TL.Second
                Label3.Text=TL.third
                Label4.Text=TL.forth
                Label5.Text=TL.five
                Label6.text=TL.six
                Exit
              
            Next
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)
                StartActivity(Main)
                Activity.Finish
            End If
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

jchal

Active Member
Licensed User
Longtime User
i know but i dont understand how to fix itm what is the eror i am doing and how can i corrected?
 
Upvote 0

eps

Expert
Licensed User
Longtime User
The tool Erel mentioned can give you a hint about the code you need to put in place to traverse your JSON correctly, using the link above and your JSON it gives the following :

B4X:
Dim parser As JSONParser 
parser.Initialize(<text>) 
Dim root As Map = parser.NextObject 
Dim draw As Map = root.Get("draw") 
Dim drawTime As String = draw.Get("drawTime") 
Dim drawNo As Int = draw.Get("drawNo") 
Dim results As List = draw.Get("results") 
For Each colresults As Int In results 
Next
 
Upvote 0
Top