B4J Question json Parsing

Declan

Well-Known Member
Licensed User
Longtime User
I get the following error when attempting to parse a json file:
B4X:
Waiting for debugger to connect...
Program started.
POST /01854fac5301ace2a54502 HTTP/1.1
content-length: 62
accept-language: fr
host: 165.73.80.252:17178
content-type: application/json
accept-encoding: gzip,deflate
user-agent: SIGFOX
accept-charset: UTF-8;q=0.9,*;q=0.7
{
  "device":"BC5974",
  "data":"01854fac5301ace2a54502"
}
Error occurred on line: 104 (Main)
java.lang.RuntimeException: JSON Object expected.
    at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:50)
    at b4j.example.main._astream_newdata(main.java:332)
    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.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    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.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    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)

The json file structure is:
B4X:
{
  "device":"{device}",
  "data":"{data}"
}
The json file is received, as can be seen in: "Log(MyStr)"
The error occurs in line: "Dim root As Map = parser.NextObject"

My Code:
B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    Dim MyStr As String
    MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")

    Log(MyStr)

    Dim parser As JSONParser
    parser.Initialize(MyStr)
    Dim root As Map = parser.NextObject
    Dim data As String = root.Get("data")
    Dim device As String = root.Get("device")
    Log(data)
    Log(device)
    
    ' Close Connection
    Disconnect
    
End Sub

Is this possibly a problem with:
Dim MyStr As String
MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
 

Declan

Well-Known Member
Licensed User
Longtime User
I don't make the request.
The data is "Pushed" from the Sigfox cloud via a Callback to my port 17178.
I listen on Port 17178 for the data.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I now parse only the json part of the http with:
B4X:
Sub AStream_NewData (Buffer() As Byte)
    
    Dim MyStr As String
    MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
    
    MyStr = MyStr.Trim
'
'    Log(MyStr)
    
    Dim strJSON As String
    Dim pos1 As Int
    Dim pos2 As Int
    
    pos1 = MyStr.IndexOf("{")
    pos2 = MyStr.LastIndexOf("}")
    strJSON=MyStr.SubString2(pos1,(pos2 + 1))
    
    Log(pos1)
    Log(pos2)
    
    Log(strJSON)

    Dim parser As JSONParser
    parser.Initialize(MyStr)
    Dim root As Map = parser.NextObject
    Dim data As String = root.Get("data")
    Dim device As String = root.Get("device")
    Log(data)
    Log(device)
    
    ' Close Connection
    Disconnect
    
End Sub
But still receive the error:
B4X:
Waiting for debugger to connect...
Program started.
229
290
{
  "device":"BC5974",
  "data":"01854fac5301ace2a54502"
}
Error occurred on line: 119 (Main)
java.lang.RuntimeException: JSON Object expected.
    at anywheresoftware.b4j.objects.collections.JSONParser.NextObject(JSONParser.java:50)
    at b4j.example.main._astream_newdata(main.java:362)
    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.shell.Shell.runMethod(Shell.java:632)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    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.shell.ShellBA.raiseEvent2(ShellBA.java:98)
    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)
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Many thanks.
Eish - case of "Cannot see the wood for the trees"
It now looks as if I am not disconnecting or releasing the connection correctly.
Sigfox is not acknowledging that I have received, or actioned, the callback.
Must I have a JobDone function?
 
Upvote 0
Top