如何将此嵌套的json放入类中 [英] How to bring this nested json into classes
问题描述
我有这个嵌套结构
,并想将其解析为类. 我有这段代码来获取json文件并反序列化
and want to parse it into classes. I have this code to get the json file and to deserialize it
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为空,我也不知道为什么.有人可以在这里帮助我吗?
But running the code the object datasourceInfo is empty and I do not know why. Anyone who can help me here?
推荐答案
您误解了JSON图标的含义.仅方括号[]
表示数组/列表.大括号{}
表示 对象 .
You've mistaken the meaning of the JSON icons. Only square brackets []
denote arrays/lists. The curly brackets {}
denote objects.
-
results
是DatasourceInfo
的列表( 不是 列表的列表).
results
is a list ofDatasourceInfo
(not a list of a list).
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
).
The DatasourceInfos
class (note the s
on the end) can be removed altogether.
这篇关于如何将此嵌套的json放入类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!