JSON(newtonsoft)在VB.net

时间:2015-12-11 15:38:39

标签: json vb.net class serialization

我正在尝试反序列化通常需要两个类的JSON字符串,我想用从MS Access字段名称获取的数组中的项替换其中一个类(请参阅注释掉的类)。

这样,如果数据库表发生变化,我就不必修改我的代码(只有Access表本身和源JSON)。

在下面的代码中,我坚持如何定义公共属性“Data”以及js.data(SampleClass.data)的定义。看我的??????问号!我试图使用List(Of String)来存储字段名称,并希望将这些列表存储在Data(0),Data(1),Data(2)等中,因此每个Data元素都有字段名称列表,和每条记录的相关值。

我想获取如下数据: js(SampleClass)包含未知数量的“数据”列表(根据JSON字符串中的数量),每个列表包含从Access获取的字段名。 然后,我想将结果插入到MS Access表中,每个“数据”列表都是一个记录。

注意:我已使用非常小的JSON字符串提取替换了“rawresp”(来自HTTP请求)。

任何人都可以帮助,非常感谢。我希望这一切都有点意义

谢谢, 阿德里安

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

1 个答案:

答案 0 :(得分:0)

数据不会仅仅是List(of Dictionary(of string, string))吗?

所以 - Data(0).Item("ABC") = "Something" ......等等。

不确定newtonsoft如何反序列化,所以这是一个经典的袖口堆栈答案(例如 - 值得你付出的代价);)