Android Question Error in extract json

Fabrice La

Active Member
Licensed User
Longtime User
My code :
B4X:
Dim job As HttpJob
    job.Initialize ("", Me)
    job.Download("https://world.openfoodfacts.org/api/v0/product/" & codebar & ".json")
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log("Job success")
        Dim parser As JSONParser
        parser.Initialize(job.GetString)
        Dim root As Map = parser.NextObject
        Dim status_verbose As String = root.Get("status_verbose")
        Dim result As List = root.Get("product")
        For Each colresult As Map In result
        Dim product_name As Double = colresult.Get("product_name")
        Next

the error in "For Each colresult As Map In result"
Error occurred on line: 622
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List
at anywheresoftware.b4a.objects.collections.List.getSize(List.java:129)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
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:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5095)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)

I have the same code in B4J without error
 

DonManfred

Expert
Licensed User
Longtime User
Post the json you want to parse. Base on the error
B4X:
Dim result As List = root.Get("product")
product seems to be NOT a List but instead it is a Map.

Maybe the api you are calling just return ONE result. But not included in a list...
 
Upvote 0
Top