如何将此嵌套的json放入类中

时间:2018-09-21 06:51:35

标签: json vb.net

我有这个嵌套结构

enter image description here

,并想将其解析为类。 我有这段代码来获取json文件并反序列化

 Public Function getDatasources(ByVal _token As String) As List(Of Results)
        Dim client = New RestClient(_baseURI)
        Dim request = New RestRequest("/datasource", Method.GET)
        request.AddHeader("Authorization", "Bearer " + _token)
        request.AddHeader("environment", _environment)
        Dim jstr = client.Execute(request).Content
        Dim datasourceInfo As List(Of Results) = JsonConvert.DeserializeObject(Of List(Of Results))(jstr)

        Return datasourceInfo

    End Function

并构建此类结构

Public Class Results
    Public Property results As List(Of DatasourceInfos)
End Class

Public Class DatasourceInfos
    Public Property DSContainer() As List(Of DatasourceInfo)
End Class

Public Class DatasourceInfo
    Public Property id As String
    Public Property name As String
    Public Property description As String
    Public Property created As ULong
    Public Property modified As ULong
    Public Property creator As List(Of Creator)
    Public Property editor As List(Of Editor)
End Class

Public Class Creator
    Public Property email As String
    Public Property login As String
End Class

Public Class Editor
    Public Property email As String
    Public Property login As String
End Class

但是运行代码后,对象datasourceInfo为空,我也不知道为什么。有人可以在这里帮助我吗?

1 个答案:

答案 0 :(得分:3)

您误解了JSON图标的含义。仅方括号[]表示数组/列表。大括号{}表示 对象

  • resultsDatasourceInfo的列表( 不是 列表的列表)。

  • DatasourceInfo.creator Creator,并且:

  • DatasourceInfo.editor Editor

您的代码应更改为:

Public Class Results
    Public Property results As List(Of DatasourceInfo)
End Class

Public Class DatasourceInfo
    ...your other properties...

    Public Property creator As Creator
    Public Property editor As Editor
End Class

可以完全删除DatasourceInfos类(注意最后的 s )。