Rushil Dahisaria
New Member
Hello,
I would be great-full and happy if someone could help me out over here. I have searched a lot in the forums and tried multiple pieces of code but unfortunately I have issues.
I want to make a widget. I get the data from a web server in Json format. I am able to get it successfully and also parse it. My json contains multiple columns and rows.
Something like this,
I also used the online json parcer tool. I can successfully extract the data.
B4A code,
The thing I want to do is display "Room name", "Device_Name","temperature" and "humidity" for all 4 rooms on a widget.
How can I do this ?
I tried but what I get is only the last values. The logs works correctly. How can I update the text in the lables ?
Please help
I would be great-full and happy if someone could help me out over here. I have searched a lot in the forums and tried multiple pieces of code but unfortunately I have issues.
I want to make a widget. I get the data from a web server in Json format. I am able to get it successfully and also parse it. My json contains multiple columns and rows.
Something like this,
B4X:
{
"locations": [
{
"name": "My Home",
"address": {
"housenumber": "3",
"country": "abc",
},
"rooms": [
{
"name": "room1",
"appliances": [
{
"name": "device1",
"config": {
"thresholds": [
{
"quantity": "temperature",
"type": "min"
}
]
},
"data_latest": {
"measurement": {
"temperature": 15.5,
"humidity": 72,
"battery": 60,
}
}
}
],
},
{
"name": "room2",
"appliances": [
{
"name": "device2",
"config": {
"thresholds": [
{
"quantity": "temperature",
"type": "min"
},
]
},
"data_latest": {
"measurement": {
"temperature": 15.5,
"humidity": 72,
"battery": 60,
}
}
}
]
},
{
"name": "room3",
"appliances": [
{
"name": "device3",
"config": {
"thresholds": [
{
"quantity": "temperature",
"type": "min"
},
]
},
"data_latest": {
"measurement": {
"temperature": 15.5,
"humidity": 72,
"battery": 60,
}
}
}
]
},
{
"name": "room4",
"appliances": [
{
"name": "device4",
"config": {
"thresholds": [
{
"quantity": "temperature",
"type": "min"
},
]
},
"data_latest": {
"measurement": {
"temperature": 15.5,
"humidity": 72,
"battery": 60,
}
}
}
]
}
]
}
]
}
I also used the online json parcer tool. I can successfully extract the data.
B4A code,
B4X:
Sub JobDone (Job As HttpJob)
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "Job"
'print the result to the logs
Log("OpenBot" & Job.GetString)
Case "Dashboard"
Log("Dashboard" & Job.GetString)
Dim parser As JSONParser
parser.Initialize(Job.GetString)
Dim root As Map = parser.NextObject
locations = root.Get("locations")
For Each collocations As Map In locations
address = collocations.Get("address")
housenumber = address.Get("housenumber")
House_Name = collocations.Get("name")
Log(House_Name)
rooms = collocations.Get("rooms")
For Each colrooms As Map In rooms
Room_Name = colrooms.Get("name")
appliances = colrooms.Get("appliances")
For Each colappliances As Map In appliances
Device_Name = colappliances.Get("name")
data_latest = colappliances.Get("data_latest")
measurement = data_latest.Get("measurement")
temperature = measurement.Get("temperature")
humidity = measurement.Get("humidity")
Log(Room_Name)
Log(Device_Name)
Log(temperature)
Log(humidity)
Next
Next
Next
End Select
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
The thing I want to do is display "Room name", "Device_Name","temperature" and "humidity" for all 4 rooms on a widget.
How can I do this ?
I tried but what I get is only the last values. The logs works correctly. How can I update the text in the lables ?
Please help
Last edited: