I have the following code that must parse a JSON string/file received from the Sigfox cloud.
The JSON file that is sent from the Sigfox cloud is:
This was assembled using the B4J Network example.
I receive the following error - and after pull hair for a few hours, I need assistance.
B4X:
Sub AStream_NewData (Buffer() As Byte)
Dim MyStr As String
MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF8")
Log(MyStr)
Dim JSON As JSONParser
Dim Map1 As Map
JSON.Initialize(MyStr)
Map1 = JSON.NextObject
Dim m As Map 'helper map for navigating
Dim MenuItems As List
m = Map1.Get("menu")
m = m.Get("popup")
MenuItems = m.Get("menuitem")
For i = 0 To MenuItems.Size - 1
m = MenuItems.Get(i)
Log(m.Get("deviceID"))
Log(m.Get("itime"))
Log(m.Get("data"))
Next
End Sub
B4X:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"deviceID":"{device}",
"itime":"{time}",
"data":"{data}"}
]
}
}}
I receive the following error - and after pull hair for a few hours, I need assistance.
B4X:
main._astream_newdata (java line: 106)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:50)
at b4j.example.main._astream_newdata(main.java:106)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$2.run(BA.java:230)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)