B4R Question JSON parsing arrays of objects

Status
Not open for further replies.

peacemaker

Expert
Licensed User
Longtime User
Hi, All
How to parse the arrays, like here ? data:[]

B4X:
{"ret":"ok","msg":"Data received OK","data":[{"id":"1","scanner_name":"de7d168a0c070820","time":"1563557285172","stamp":"19.07.2019 21:28:05.172 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"2","scanner_name":"de7d168a0c070820","time":"1563557599242","stamp":"19.07.2019 21:33:19.242 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"3","scanner_name":"de7d168a0c070820","time":"1563786327433","stamp":"22.07.2019 13:05:27.433 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"4","scanner_name":"de7d168a0c070820","time":"1563786327433","stamp":"22.07.2019 13:05:27.433 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"5","scanner_name":"de7d168a0c070820","time":"1563788290187","stamp":"22.07.2019 13:38:10.187 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"6","scanner_name":"de7d168a0c070820","time":"1563788403374","stamp":"22.07.2019 13:40:03.374 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"7","scanner_name":"de7d168a0c070820","time":"1563786327433","stamp":"22.07.2019 13:05:27.433 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"8","scanner_name":"de7d168a0c070820","time":"1563788290187","stamp":"22.07.2019 13:38:10.187 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"9","scanner_name":"de7d168a0c070820","time":"1563788403374","stamp":"22.07.2019 13:40:03.374 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"},{"id":"10","scanner_name":"de7d168a0c070820","time":"1563808802600","stamp":"22.07.2019 19:20:02.600 (+04:00)","command":"W","val":"1.5","replay":"","received":"0"}]}
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Very simple using: JSON Parsing

B4X:
Do While True
    GetTextValueFromKey(jsontext, "id", LastIndex, buffer, MaxSize)
    If buffer.Length = 0 Then Exit
    Log("id: ", buffer)
    GetTextValueFromKey(jsontext, "time", LastIndex, buffer, MaxSize) 'order of elements is important as it goes forward all the time
    Log("time: ", buffer)
Loop
Log("end...")
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Indeed, thanks, Erel !

I was trying

B4X:
Do Until buffer = ""
                    GetTextValueFromKey(Job.Response, "command", LastIndex, buffer, MaxSize)
                    Dim command() As Byte = buffer
                    Log("buffer:", buffer)
                    GetTextValueFromKey(Job.Response, "val", LastIndex, buffer, MaxSize)
                    Dim value As String = Main.bc.StringFromBytes(buffer)
                    Log("buffer:", buffer)
                    Loop
But it was not correct.
 
Upvote 0

RogerW

Member
When I enter the above code I have a problem with:-
code:
GetTextValueFromKey(res, "id", LastIndex, buffer, MaxSize)

It says GetTextValueFromKey is an undeclared variable, I assume this means that I am missing a library reference? Can you let me know which library I need to reference, or could you let me know where I can look up the functions to find out which library they reside in.

Thanks.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
 
Upvote 0
Status
Not open for further replies.
Top