我有这个嵌套结构
,并想将其解析为类。 我有这段代码来获取json文件并反序列化
Public Function getData(ByVal _token As String, ByVal _identifier As String) As Results_FullData
Dim client = New RestClient(_baseURI)
Dim request = New RestRequest("/datasource/{id}/data", Method.GET)
request.AddParameter("id", _identifier)
request.AddUrlSegment("id", _identifier)
request.AddHeader("Authorization", "Bearer " + _token)
request.AddHeader("environment", _environment)
Dim jstr = client.Execute(request).Content
Dim allData As Results_FullData = JsonConvert.DeserializeObject(Of Results_FullData)(jstr)
Return allDATA
End Function
并构建此类结构
Public Class Results_FullData
Public Property results As List(Of DSContent)
End Class
Public Class DSContent
Public Property userRunId As Long
Public Property metaColumnValues As List(Of String)
Public Property dataColumnValues As List(Of String)
End Class
但是运行代码后,对象datasourceInfo为空,我也不知道为什么。我以为我可以采用this answer的解决方案,但是它不起作用。我猜List(Of String)
部分是错误的。问题可能是每个对象{}
中metaColumnValues和dataColumnValues的长度不同。因为将值,
分隔在对象中,所以我们的想法是将其分成字符串并在以后进行分隔
有人可以在这里帮助我吗?
编辑: 数据示例:
{"result":[{"userRunId":"xxxxxxx","metaColumnValues":["9006409","20073"],"dataColumnValues":["","superior"]},{"userRunId":"xxxxxxx","metaColumnValues":["2345","235","1"],"dataColumnValues":["","superior", "test"]}]}
答案 0 :(得分:1)
在Results_FullData
中,该属性称为results
,但是在示例JSON中,它称为result
。此外,DSContent.userRunId
被声明为Long
,即使在JSON中,该属性也包含String
值。如果您在数据类中修复了这两件事,它将正确地反序列化示例数据:
Public Sub Main()
Dim json As String = "{""result"":[{""userRunId"":""xxxxxxx"",""metaColumnValues"":[""9006409"",""20073""],""dataColumnValues"":["""",""superior""]},{""userRunId"":""xxxxxxx"",""metaColumnValues"":[""2345"",""235"",""1""],""dataColumnValues"":["""",""superior"", ""test""]}]}"
Dim allData As Results_FullData = JsonConvert.DeserializeObject(Of Results_FullData)(json)
End Sub
Public Class Results_FullData
Public Property result As List(Of DSContent)
End Class
Public Class DSContent
Public Property userRunId As String
Public Property metaColumnValues As List(Of String)
Public Property dataColumnValues As List(Of String)
End Class