Hello,
I'm trying to get a JSON data to a dropdown B4Xcombobox, but not succeeding.
I also tried to change the maptoarray via a sub found on the forum.
Created the json parser via the online tool.
JSON is simple like: [{"id":"31","albumname":"Album1"},{"id":"32","albumname":"Album2"},{"id":"33","albumname":"Album3"},{"id":"34","albumname":"Album4"}]
Found below sub here: https://www.b4x.com/android/forum/threads/convert-map-to-array.141176/#content
Maybe the Sub mapToArray is not needed at all, but I've tried for days, But not succeeding, getting different errors on the way trying to solve.
Or is this easier to do without JSON? Just getting the two values per line and add it to the dropdownmenu?
Thanks for any help.
Best regards
I'm trying to get a JSON data to a dropdown B4Xcombobox, but not succeeding.
I also tried to change the maptoarray via a sub found on the forum.
Created the json parser via the online tool.
JSON is simple like: [{"id":"31","albumname":"Album1"},{"id":"32","albumname":"Album2"},{"id":"33","albumname":"Album3"},{"id":"34","albumname":"Album4"}]
B4X:
Sub JobDone (job As HttpJob)
Select job
Case readdataALBUMS
'B4XComboBox1Albums.cmbBox.Clear
'Dim AlbumsInfo() As String
ListOfAlbums.Initialize
If job.Success Then
Dim res As String
res = job.GetString
Log("Response from server albums: " & res)
Dim parser As JSONParser
parser.Initialize(res)
Dim jRoot As List = parser.NextArray
Log("jRoot: " & jRoot)
For Each coljRoot As Map In jRoot
Dim albumname() As String = coljRoot.Get("albumname")
Dim id() As String = coljRoot.Get("id")
Log("albumname: " & albumname & " id: " & id)
Dim a() As String=mapToArray(albumname)
Log("a= " & a(1))
B4XComboBox1Albums.cmbBox.Add(a(0))
Next
Else
Log("ERROR: " & job.ErrorMessage)
End If
readdataALBUMS.Release
'other case not needed for this question
End Select
End Sub
Found below sub here: https://www.b4x.com/android/forum/threads/convert-map-to-array.141176/#content
B4X:
public Sub mapToArray(m As Map) As Object()
Dim arr(m.Size * 2) As Object 'array size is double of the map because both keys and values are elements
Dim i As Int=0
Dim value As Object
For Each key As Object In m.Keys
value=m.Get(key)
LogColor($"key=${key}, value=${value}"$, Colors.blue)
arr(i)=key
i = i +1
arr(i)=value
i =i +1
Next
Return arr
End Sub
Maybe the Sub mapToArray is not needed at all, but I've tried for days, But not succeeding, getting different errors on the way trying to solve.
Or is this easier to do without JSON? Just getting the two values per line and add it to the dropdownmenu?
Thanks for any help.
Best regards