B4R Question What is going on here when parsing this JSON?

hatzisn

Well-Known Member
Licensed User
Longtime User
Good afternoon everyone,

I have created a test project which demonstrates a problem I am facing parsing some JSON. I cannot figure out what is going on here, why it is changing the SSID Byte Array and further more how to fix that. If anyone can help it would be highly appreciated.


Thanks in advance
 

Attachments

  • TestJSON.zip
    2.2 KB · Views: 138

Daestrum

Expert
Licensed User
Longtime User
It looks like its the trying to use ResultBuffer to get the value back.

I chopped your source a bit to concentrate on getting the value back, but it should be sufficient for you to add in the parts I cut out.

In the main routine I ended up with
B4X:
    Dim SSID() As Byte = JSON.GetTextValueFromKey(sJSON.GetBytes, "ssid".GetBytes, Null, 0)

     Dim PASS() As Byte = JSON.GetTextValueFromKey(sJSON.GetBytes, "pass", Null, 0)

    Log("ssid: ", SSID, " pass: ",PASS)

In the json module I ended up with (this is where a lot of original code is missing)
B4X:
Sub GetTextValueFromKey (json() As Byte, Key() As Byte, primKey() As Byte, StartIndex As Long) As Byte()
    Dim bc As ByteConverter

    Dim resultbuff() As Byte

    Dim qkey() As Byte = JoinBytes(Array(quotearray, Key, quotearray))

    Dim i As Int = bc.IndexOf2(json, qkey, StartIndex)

    If i = -1 Then
        bc.ArrayCopy(Array As Byte(), resultbuff)
        Return resultbuff
    End If

    Dim i1 As Int = i + qkey.Length + 2

    Dim i2 As Int = bc.IndexOf2(json, quotearray, i1 + 1)

    Log("data ",bc.SubString2(json,i1,i2))
    Dim s() As Byte = bc.SubString2(json,i1,i2)
    Log("s : ",s)
    bc.ArrayCopy(s, resultbuff)
    Log("resBuff : ",resultbuff)
    Return resultbuff
End Sub

it now displays
1623938130036.png


Hope it helps.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thank you Daestrum for your effort. It works this way. I checked it also for memory leakage and it is trustworthy.

I do not understand though. ResultBuffer is a Byte Array defined in the GetTextValueFromKey scope. We pass to it the SSID byte array which is object one and changes by reference its value. Correctly? Then we pass to it the PASS byte array which is object two (an other object) and by reference it changes again the value. Since the scope of ResultBuffer is in GetTextValueFromKey scope how does it remain "alive" as a different object?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I don't think ResultBuffer actually exists, it's just a pointer to the variable you called the routine with.

It reminded me of the TableView conundrum if you dont dim the item inside the fill table loop all items end up as the last item added.
 
Upvote 0
Top