Hi
I am trying to create a map as per one of your examples but I need to loop in the create
where ID and Qty will be coming from a table
I have been looking all over for an example and just cannot seem to find one
I would appreciate if someone could help
Thank you
Dim jg As JSONGenerator
Dim m As Map = CreateMap("itemslist": CreateMap( _
over here loop with "itemID": ID,"Qty": Qnty, _
))
You can't do this with a single line of code.
Simply create the second Map and then add it to the first one.
B4X:
Do While ResultSet1.NextRow
ID = ResultSet1.GetInt("ID")
Qty = ResultSet1.GetDouble("Qty")
mData.Put(ID, Qty)
Loop
mMainMap.Put("itemslist", mData)
ResultSet1.Close
(I didn't declare nor initialize the variables, it's just an example)
If you really want to give a name to those elements, create a custom type.
B4X:
Type tItem(itemID As Int, Qty As Double)
Hover over the name of your custom type and let the editor generate the function: CreatetItem (Sub).
Then:
B4X:
Do While ResultSet1.NextRow
ID = ResultSet1.GetInt("ID")
Qty = ResultSet1.GetDouble("Qty")
Dim Item As tItem = CreatetItem(ID, Qry)
mData.Put(ID, Item)
Loop
mMainMap.Put("itemslist", mData)
ResultSet1.Close
(but you will waste memory, as the ID value will be used twice, as a Map key and as a value)
Thank you for all your help, but the answer i was really looking for was an example posted on one of the threads after extensive searching
I modified it to this to work for me, so Thanks again
B4X:
Dim Items As List
Items.Initialize
Do While ResultSet1.NextRow
ID = ResultSet1.GetInt("ID")
Qty = ResultSet1.GetDouble("Qty")
Items.Add(CreateMap("itemid": ID, "qty": Qty))
Loop
Dim Data As Map = CreateMap("Version": 1.00, "ID": "abcdef","Items": Items)
Dim jg As JSONGenerator
jg.Initialize(Data)
Dim JsonString As String = jg.ToPrettyString(4)
EditText1.Text = JsonString