Java Question how to Pass a JsonObject from java library to my b4a app ?

Addo

Well-Known Member
Licensed User
Longtime User
hello everyone hopefully everything is well, I have a jsonarray in my android studio java Library and i am trying to pass it as a parameter.

I thought that it is better to pass it as string like following

Java:
 if (Newparticipant != null && Newparticipant.length() > 0) {
 BA.Log("------New User-----");
 rtcEvents.onNewParticipant(Newparticipant.toString());
}

// then i call it in b4a event like following
 public void onNewParticipant(String participantJson) {
   B4a.raiseEventFromDifferentThread(Globalunit.this,null,0,eventName+"_onnewparticipant", false, new Object[] {participantJson});
 }

then on My B4a app I do

B4X:
Sub MywsClient_onNewParticipant(participant As String)
    Dim parser As JSONParser
    parser.Initialize(participant)
    Dim NewUser As Map = parser.NextObject
    Log(NewUser.Size)
End Sub


i got this output when i do log participant parameter

B4X:
[{"id":2302024264352889,"display":"B4xUser","setup":false,"muted":false}]

then this error
[{"id":2302024264352889,"display":"B4xUser","setup":false,"muted":false}]
myservice_mywsclient_onnewparticipant (java line: 156)
java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:67)
at b4a.example.myservice._mywsclient_onnewparticipant(myservice.java:156)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8387)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
java.lang.RuntimeException: java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:250)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8387)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
Caused by: java.lang.RuntimeException: JSON Object expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:67)
at b4a.example.myservice._mywsclient_onnewparticipant(myservice.java:156)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
... 8 more
it says that Json expected which i already passed Jsonarray to string. what is my mistake ?
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
[{"id":2302024264352889,"display":"B4xUser","setup":false,"muted":false}]

Contains a JSON-LIST of Maps, not just a Map!

B4X:
Dim parser As JSONParser
parser.Initialize("[{"id":2302024264352889,"display":"B4xUser","setup":false,"muted":false}]")
Dim jRoot As List = parser.NextArray
For Each coljRoot As Map In jRoot
 Dim display As String = coljRoot.Get("display")
 Dim setup As String = coljRoot.Get("setup")
 Dim id As String = coljRoot.Get("id")
 Dim muted As String = coljRoot.Get("muted")
Next
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
    Dim json As String = $"[{"id":2302024264352889,"display":"B4xUser","setup":false,"muted":false}]"$
    
    Dim parser As JSONParser
    parser.Initialize(json)
    If (json.StartsWith("[") And json.EndsWith("]")) Then
        Dim jRoot As List = parser.NextArray
        For Each coljRoot As Map In jRoot
            Dim display As String = coljRoot.Get("display")
            Dim setup As String = coljRoot.Get("setup")
            Dim id As String = coljRoot.Get("id")
            Dim muted As String = coljRoot.Get("muted")
        Next
    Else If (json.StartsWith("{") And json.EndsWith("}")) Then
        Dim coljRoot As Map = parser.NextObject
        Dim display As String = coljRoot.Get("display")
        Dim setup As String = coljRoot.Get("setup")
        Dim id As String = coljRoot.Get("id")
        Dim muted As String = coljRoot.Get("muted")
    End If
 
Top