Android Question JSON - create from object

PHB2

Member
Licensed User
Longtime User
I have a class object defined as:

B4X:
Dim GUID As String
Dim itemName As String
Dim itemType As String
Dim createdDate As Object
Dim ownerGUID As String
Dim transactionGUID As String
Dim transactionType As String
Dim transactionDate As Object
Dim fromUserGUID As String
Dim forListGUID As String  
Dim forItem2GUID As String
Dim notes As String
Dim DataItems As List

My code to create JSON from this class is:

B4X:
Dim m As Map
m.Initialize
 
m.Put( "GUID", GUID )
m.Put( "itemName", itemName )
m.Put( "itemType", itemType )
m.Put( "createdDate", createdDate )
m.Put( "ownerGUID", ownerGUID )
m.Put( "transactionGUID", transactionGUID )
m.Put( "transactionType", transactionType )
m.Put( "transactionDate", transactionDate )
m.Put( "fromUserGUID", fromUserGUID )
 
m.Put( "forListGUID", forListGUID )
m.Put( "forItem2GUID", forItem2GUID )
m.Put( "notes", notes )
 
' this may need some work!
Dim Map2 As Map
Map2.Initialize
For i=0 To DataItems.Size-1
   Map2.Put("",DataItems.Get(i) )
Next
 
m.Put("DataItems",Map2 )

Dim JSONGenerator1 As JSONGenerator
JSONGenerator1.Initialize( m )
         
Dim json As String
json = ""
json = JSONGenerator1.ToString

I am calling my function above with the following code
B4X:
    Dim e As ExerciseObject
    e.Initialize
   
    ' create json from object
    e.itemName = "Test 1"
   
    e.DataItems.Initialize
    e.DataItems.Add("di 1")
    e.DataItems.Add("di 2")
    e.DataItems.Add("di 3")
    e.DataItems.Add("di 4")
   
    Dim json As String
    json = e.CreateJSONFromObject

It may be the hayfever clouding my brain, but I simply cannot work out how to create the JSON correctly with respect to the DataItems List. It seems to produce malformed JSON.

Any assistance would be gratefully received.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Dim Map2 As Map
Map2.Initialize
For i=0 To DataItems.Size-1
Map2.Put("",DataItems.Get(i) )
Next
B4X:
    Dim Map2 As Map
    Map2.Initialize
    For Each key As String In dataitems.Keys
        Map2.Put(key,dataitems.Get(key) )
    Next
 
Upvote 0
Top