B4J Question How to populate Tableview with a json string

saeed10051

Active Member
Licensed User
Longtime User
I am running jokhttputils2 and json parser to get a json string from an online database.
I have a Tableview on my java app which i want to populate with the data received in the json string.
I am running following code for that
B4X:
Sub loadlistview
Dim Table As List
Dim p As JSONParser
Dim res As String
Dim j As HttpJob
Dim m1 As Map
j.Initialize("", Me)
j.Download("http://saeedhassan.atwebpages.com/r.php")
Wait For (j) jobdone (j As HttpJob)
If j.Success Then
    res = j.GetString
    Log(res)
    p.Initialize(res)
        Table = p.NextArray
        TableView1.SetColumns(Array As String("ID", "NAME", "PRICE"))
        TableView1.Items.Add(p.NextArray)
in the log i can see that i have got the following json string back from online database

[{"itemid":"1","itemname":"chicken biryani","itemprice":"20"},{"itemid":"2","itemname":"mutton biryani","itemprice":"25"},{"itemid":"3","itemname":"rice","itemprice":"10"},{"itemid":"4","itemname":"chicken qorma","itemprice":"15"},{"itemid":"5","itemname":"mutton qorma","itemprice":"25"},{"itemid":"6","itemname":"naan","itemprice":"2"},{"itemid":"7","itemname":"vegetable","itemprice":"8"},{"itemid":"8","itemname":"daal","itemprice":"8"},{"itemid":"9","itemname":"egg","itemprice":"5"},{"itemid":"10","itemname":"egg sandwich","itemprice":"10"},{"itemid":"11","itemname":"tea","itemprice":"1"},{"itemid":"12","itemname":"coffee","itemprice":"3"},{"itemid":"13","itemname":"sweet","itemprice":"10"},{"itemid":"14","itemname":"donut","itemprice":"5"},{"itemid":"15","itemname":"water","itemprice":"1"},{"itemid":"16","itemname":"pizza","itemprice":"30"},{"itemid":"17","itemname":"ice cream","itemprice":"7"}]

however the tableview is not getting populated it is giving me a casting error as i am getting a list of map items from onlie server, i have tried to put the Table list also in Tableview but it is also giving error
 

DonManfred

Expert
Licensed User
Longtime User
[{"itemid":"1","itemname":"chicken biryani","itemprice":"20"},{"itemid":"2","itemname":"mutton biryani","itemprice":"25"},{"itemid":"3","itemname":"rice","itemprice":"10"},{"itemid":"4","itemname":"chicken qorma","itemprice":"15"},{"itemid":"5","itemname":"mutton qorma","itemprice":"25"},{"itemid":"6","itemname":"naan","itemprice":"2"},{"itemid":"7","itemname":"vegetable","itemprice":"8"},{"itemid":"8","itemname":"daal","itemprice":"8"},{"itemid":"9","itemname":"egg","itemprice":"5"},{"itemid":"10","itemname":"egg sandwich","itemprice":"10"},{"itemid":"11","itemname":"tea","itemprice":"1"},{"itemid":"12","itemname":"coffee","itemprice":"3"},{"itemid":"13","itemname":"sweet","itemprice":"10"},{"itemid":"14","itemname":"donut","itemprice":"5"},{"itemid":"15","itemname":"water","itemprice":"1"},{"itemid":"16","itemname":"pizza","itemprice":"30"},{"itemid":"17","itemname":"ice cream","itemprice":"7"}]
This is the code to parse the json

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
 Dim itemid As String = colroot.Get("itemid")
 Dim itemname As String = colroot.Get("itemname")
 Dim itemprice As String = colroot.Get("itemprice")
Next

You need to build the data for Tableview by yourself
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
This is the code to parse the json

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim itemid As String = colroot.Get("itemid")
Dim itemname As String = colroot.Get("itemname")
Dim itemprice As String = colroot.Get("itemprice")
Next

You need to build the data for Tableview by yourself
Thanks Donmanfred i have done that type of data mining in a B4A program but i dont understand how to put these individual values in the Tableview, as it is asking for an object in TableView1.Items.Add
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
It is unclear for me which Library you are using exactly.

How is the example code for tableview adding the Data to the Tableview?
I am using this tableview example. Here they are using a sub create row for populating the Tableview.
 

Attachments

  • TableExample.zip
    2.8 KB · Views: 272
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
thanks DonManfred, let me try that. Actually there is a gap of ten years, i have recently again started using B4x
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
I suggest to watch some Vieos then.

And/Or read @klaus Booklets

I am running following code. it is populating the table view as intended
B4X:
Sub loadlistview
Dim Table As List
Dim p As JSONParser
Dim res As String
Dim j As HttpJob
Dim m1 As Map
j.Initialize("", Me)
j.Download("http://saeedhassan.atwebpages.com/r.php")
Wait For (j) jobdone (j As HttpJob)
If j.Success Then
    res = j.GetString
    Log(res)
    p.Initialize(res)
        Table = p.NextArray
        TableView1.SetColumns(Array As String("ID", "NAME", "PRICE"))
            
        For i = 0 To Table.Size-1
            m1 = Table.Get(i)
            Dim row() As Object
            row = Array As String(m1.Get("itemid"),m1.Get("itemname"),m1.Get("itemprice"))
            TableView1.Items.Add(row)
        Next
End If
 
Upvote 0
Top