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

Addo

Well-Known Member
Licensed 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
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
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…