Rusty Well-Known Member Licensed User Longtime User Aug 1, 2018 #1 Parsing a JSON string is demonstrated very well in Erel's Tutorial. I would like to create JSON strings from my data. Here is an example that I cannot get to work, obviously there are some errors, but I don't see them... Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Dim Data As List Data.Initialize Data.Add("Version: 1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next Data.Add(Map1) JSONGenerator.Initialize(Data) The above code generates the following error: ** Activity (main) Create, isFirst = true ** Error occurred on line: 74 (Main) java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap at anywheresoftware.b4a.objects.collections.JSONParser$JSONGenerator.Initialize(JSONParser.java:132) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:780) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:363) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.samples.json.main.afterFirstLayout(main.java:104) at anywheresoftware.b4a.samples.json.main.access$000(main.java:17) at anywheresoftware.b4a.samples.json.main$WaitForLayout.run(main.java:82) 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:5021) 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:827) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method) ** Activity (main) Resume ** Click to expand... Advice is always appreciated Rusty
Parsing a JSON string is demonstrated very well in Erel's Tutorial. I would like to create JSON strings from my data. Here is an example that I cannot get to work, obviously there are some errors, but I don't see them... Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Dim Data As List Data.Initialize Data.Add("Version: 1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next Data.Add(Map1) JSONGenerator.Initialize(Data) The above code generates the following error: ** Activity (main) Create, isFirst = true ** Error occurred on line: 74 (Main) java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap at anywheresoftware.b4a.objects.collections.JSONParser$JSONGenerator.Initialize(JSONParser.java:132) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:780) at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:363) at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144) at anywheresoftware.b4a.samples.json.main.afterFirstLayout(main.java:104) at anywheresoftware.b4a.samples.json.main.access$000(main.java:17) at anywheresoftware.b4a.samples.json.main$WaitForLayout.run(main.java:82) 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:5021) 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:827) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method) ** Activity (main) Resume ** Click to expand... Advice is always appreciated Rusty
KMatle Expert Licensed User Longtime User Aug 1, 2018 #2 You initialize it with "data" which is a list. Initialize is for maps only, initialize2 for list's. Upvote 0
Rusty Well-Known Member Licensed User Longtime User Aug 1, 2018 #3 Thanks KMatle For those curious: Add a LIST: Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Dim Data As List Data.Initialize Map1.put("Version:","1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next Data.Add(Map1) JSONGenerator.Initialize2(Data) Add MAPS: Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Map1.put("Version:","1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next JSONGenerator.Initialize(Map1) Thanks for your help! Rusty Upvote 0
Thanks KMatle For those curious: Add a LIST: Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Dim Data As List Data.Initialize Map1.put("Version:","1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next Data.Add(Map1) JSONGenerator.Initialize2(Data) Add MAPS: Code: Copy 'generate JSON Dim Map1 As Map Map1.Initialize Dim JSONGenerator As JSONGenerator Map1.put("Version:","1.1") For i = 0 To 30 Map1.Put(i, "value " & i) Next JSONGenerator.Initialize(Map1) Thanks for your help! Rusty