JSON block decoder for a Datasnap REST server

mskeels

Member
Licensed User
Longtime User
I wrote a decoder for a JSON block returned by call through HTTPUtils2 to a Datasnap server. I would appreciate any comments concerning corrections/improvements to this code.

I am especially interested in comments about where to place this code, ie, should it ideally go in a service module, I suppose? And how to make it more universal.

All comments welcome and appreciated.

************

B4X:
Sub JobDone(Job As HttpJob)
   Dim JSONJobProc As JSONParser
   Dim MAPTbl As Map
   Dim m As Map    'helper map for navigating
   Dim l As List   'helper list
   Dim ListTable As List
   Dim ListColumnList As List
   Dim ListTableRows As List
   Dim strCSV As String
   Dim ListColumnArray(100) As List

   'decode job to process   
   Select Job.JobName
      'retrieve and decode a JSON block generated by Delphi Datasnap Rest server containing table data
      'place it in a List with the first row containing column IDs
      Case "GetJobs"
         'parse down to table columns
         sJSONData = Job.GetString
         edtResult.Text = Job.GetString
         JSONJobProc.Initialize(sJSONData)
         MAPTbl = JSONJobProc.NextObject
         ListTable = MAPTbl.Get("result")
         'Load table columns and value arrays
         For i = 0 To ListTable.Size - 1
            'Clear data arrays
            For k = 0 To 99
               If ListColumnArray(k).IsInitialized Then
                  ListColumnArray(k).Clear
               End If
            Next
            m = ListTable.Get(i)
            ListColumnList = m.Get("table")    'ListColumnList now holds a list of the columns arrays for each column in the table
            ListColumnArray(0).Initialize
            'for each column in the table.....
            For j = 0 To ListColumnList.Size - 1
               l = ListColumnList.Get(j)            'load the column array into a temporary map         
               'add the column name
               ListColumnArray(0).Add(l.get(0))      'use the map to get the column name
                                             'and store it in array 0; 0 always contains column names
               'place column data in the array corresponding to the column name array index + 1
               'so first column data always starts in array 2, etc.
               ListColumnArray(j+1) = m.Get(l.get(0))   'load column values array for this column
            Next
         Next
         ListTableRows.Initialize
         strCSV = ""
         'build column name string and add it 
         For i = 0 To ListColumnArray(0).Size-1
            strCSV = strCSV & ListColumnArray(0).Get(i)
            If i < (ListColumnArray(0).Size-1) Then
               strCSV = strCSV & ","
            End If   
         Next         
         ListTableRows.Add(strCSV)
         
         'for each row
         For j = 0 To ListColumnArray(1).Size-1
            strCSV = ""
            'for each column
            For i = 0 To ListColumnArray(0).Size-1
               strCSV = strCSV & ListColumnArray(i+1).Get(j)
               If i < (ListColumnArray(0).Size-1) Then
                  strCSV = strCSV & ","
               End If   
            Next      
            ListTableRows.Add(strCSV)
         Next
         edtResult.Text = ""
         ListView1.Clear
         For i = 0 To ListTableRows.Size-1
            ListView1.AddSingleLine(ListTableRows.Get(i))
         Next
      Case "SomethingElse"
   End Select
   Job.Release
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…