I am trying to deserialize a JSON string which would normally require two classes, I want to substitute one of the classes (see commented out class) with items in an array obtained from MS Access field names.
In this way I wouldn't have to modify my code should the database table change (only the Access table itself and the source JSON).
In my code below, I am stuck on how to define the Public Property "Data" and also the definition of js.data (SampleClass.data). See my ?????? question marks! I am trying to use List (Of String) to store the field names and want to store these lists in Data(0), Data(1), Data(2), etc, so each Data element has the list of field names, and associated values for each record.
I want to obtain my data as follows : js (SampleClass) contains an unknown number of "Data" lists (according to quantity found in JSON string), each list containing the fieldnames obtained from Access. I then want to insert the results into the MS Access table with each "Data" list being a record.
Note: I have replaced what would be "rawresp" (from HTTP request) with a very small extract of the JSON string.
Can anyone help please, it'd be much appreciated. I hope this all makes a little bit of sense
Thanks, Adrian
Imports Newtonsoft.Json
'link to create classes
'http://www.httputility.net/json-to-csharp-vb-typescript-class.aspx
'example of json script
'http://stackoverflow.com/questions/21676708/simple-working-example-of-json- net-in-vb-net
'Public Class Datum
'Public ABC As String
'Public DEF As String
'Public GHI As Integer
'Public JKL As Integer
'Public MNO As String
'Public PQR As String
'Public STU As String
'Public VWX As String
'Public YZA As String
'Public BCD As Integer
'Public EFG As Integer
'Public HIJ As String
'Public KLM As String
'Public NOP As String
'Public QRS As String
'End Class
Public Class SampleClass
Public Property Success As Boolean
?????? Public Property Data As List(Of String)
End Class
Module JSON
Sub getjson()
'Try
cleanup(Nothing, Nothing)
Dim daoEngine As New dao.DBEngine, db As dao.Database
db = daoEngine.OpenDatabase(DBPath)
Dim rst As dao.Recordset = Nothing
rst = db.OpenRecordset("tUnit") 'open table
Dim fields(rst.Fields.Count) As String
For j = 0 To rst.Fields.Count
fields(j) = rst.Fields(j).Name
Next
Dim js As New SampleClass
?????? Dim js.data As List(Of js.Data)(fields)
Dim rawresp As String
Dim request As Net.HttpWebRequest
Dim response As Net.HttpWebResponse = Nothing
Dim reader As IO.StreamReader
request = DirectCast(Net.WebRequest.Create("http://10.32.27.17/api/endusers"), Net.HttpWebRequest)
response = DirectCast(request.GetResponse(), Net.HttpWebResponse)
reader = New IO.StreamReader(response.GetResponseStream())
rawresp = reader.ReadToEnd()
js = JsonConvert.DeserializeObject(Of SampleClass)("{""success"":true,""data"":[{""ABC"":""Something"",""DEF"":""Other""},{""ABC"":""This"",""DEF"":""That""}]}")
?????? For Each i As String In js.Data
rst.MoveFirst()
Do While Not rst.EOF 'look through to end of table
If n(rst(i(1)).Value) Like i(1) Then
For Each j In i
rst.Edit()
rst(j).Value = j
rst.Update()
Next
End If
rst.MoveNext() 'move to next record
Loop
Next
cleanup(db, Nothing)
'Catch ex As Exception
' Debug.Print(ex.Message)
'End Try
End Sub
End Module