JSON格式反序列化错误

时间:2016-10-17 21:04:09

标签: json vb.net json.net json-deserialization

我是处理通过Web API检索的JSON格式数据的新手。我使用Newtonsoft.Json.JsonConvert反序列化并分配给datatable。

这是我试图进入数据表的JSON数据。

[{
"classDesc":"SIDEWALK,DRIVEWAY,CURB",
"classCode":"EH",
"legend":"017",
"isActive":"Y",
"atrSpaCityDistrictId":"00D17209F8F25F6D4A00011302",
"atrSpaCitieDistrict":{
"cityDistrict":"",
"isActive":"1",
"atrSpaClassLegends":null,
"id":"00D17209F8F25F6D4A00011302"
},
"id":"00D1748B8DA0AB0A7400011202"
}]

我创建了以下类

Public Class RootObject
    Public Property classDesc As String
    Public Property classCode As String
    Public Property id As Integer   
    Public Property legend As Integer
    Public Property isActive As String
    Public Property atrSpaCityDistrictId As Integer
    Public Property atrSpaCitieDistrict As List(Of Result) End Class

Public Class Result    
    Public Property cityDistrict As String
    Public Property isActive As Integer
    Public Property atrSpaClassLegends As List(Of Legend)
    Public Property id As Integer
End Class

Public Class Legend
End Class

以下是我的VB.Net代码:

Dim table As DataTable Dim client As New HttpClient() client.BaseAddress = New Uri("http://localhost:5000/") client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json")) 
Dim response As HttpResponseMessage = client.GetAsync("api/spaclasslegend").Result 
If response.IsSuccessStatusCode Then 
    Dim json As String = response.Content.ReadAsStringAsync().Result table = JsonConvert.DeserializeObject(Of DataTable)(json) 
End If

获得以下错误

  

"无法将当前JSON数组(例如[1,2,3])反序列化为类型   ' Alco.APTIS.Services.RootObject'因为类型需要JSON   对象(例如{" name":" value"})正确反序列化。"

我尝试过使用stackoverflow.com解决此问题的不同方法,最后在此处发帖寻求帮助。

1 个答案:

答案 0 :(得分:1)

你有两个不相关的问题。

首先,您的根对象与您的JSON不匹配。如果我将您的JSON复制到代码生成器(例如http://jsonutils.com/)中,我会使用HAVING获取以下List(Of Legend)属性:

atrSpaClassLegends

完成此操作后,现在可以将您的JSON反序列化为 Public Class AtrSpaCitieDistrict Public Property cityDistrict As String Public Property isActive As String Public Property atrSpaClassLegends As List(Of Legend) Public Property id As String End Class Public Class RootObject Public Property classDesc As String Public Property classCode As String Public Property legend As String Public Property isActive As String Public Property atrSpaCityDistrictId As String Public Property atrSpaCitieDistrict As AtrSpaCitieDistrict Public Property id As String End Class Public Class Legend End Class

List(Of RootObject)

请注意,您必须反序列化为Dim root = JsonConvert.DeserializeObject(Of List(Of RootObject))(jsonString) (或类似的.Net集合,例如数组),因为您的顶级JSON容器是JSON数组 - 由{{1包围的有序逗号分隔的值序列}和List(Of RootObject)。这在Json.NET Serialization Guide中有解释。示例fiddle

其次,尽管创建了这些类型,但在您的问题中显示的代码中,您根本就没有使用它们!相反,您将反序列化为[。不幸的是,]属性是嵌套的对象

DataTable

Json.NET不支持将复杂对象的列值自动反序列化为无类型数据表,如果遇到异常数据表则会抛出异常。相反,您需要反序列化为类型化数据表。但是,这可能是你问题中的拼写错误;异常错误消息表明您实际上正在尝试反序列化为atrSpaCitieDistrict类型的对象而不是 { "atrSpaCitieDistrict": { "cityDistrict": "", "isActive": "1", "atrSpaClassLegends": null, "id": "00D17209F8F25F6D4A00011302" }, }