我正在使用JSON.NET来反序列化来自HTTP查询的JSON响应,但是我遇到了一个问题。这是因为响应可以在相同属性下发送两种类型的对象,如下所示:
第一种情况样本(最常见):
{
"type": "myType",
"tid": 4,
"action": "myAction",
"method": "myMethod",
"result": {
"success": true,
"total": 4,
"records": [
{
"id": 4,
"nome": "PRIMEIRO NOME",
"sigla": "PN"
},
{
"id": 1974,
"nome": "SEGUNDO NOME",
"sigla": "SN"
},
{
"id": 2584,
"nome": "TERCEIRO NOME",
"sigla": "TN"
},
{
"id": 1170,
"nome": "QUARTO NOME",
"sigla": "QN"
}
]
}
}
第二例样品(稀有):
{
"type": "myType",
"tid": 3,
"action": "myAction",
"method": "myMethod",
"result": [
{
"id": 4,
"nome": "PRIMEIRO NOME",
"sigla": "PN"
},
{
"id": 1974,
"nome": "SEGUNDO NOME",
"sigla": "SN"
},
{
"id": 2584,
"nome": "TERCEIRO NOME",
"sigla": "TN"
},
{
"id": 1170,
"nome": "QUARTO NOME",
"sigla": "QN"
}
]
}
我一直在使用这些类来接收第一种情况的数据:
Public Class Record
Public Property id As Integer
Public Property nome As String
Public Property sigla As String
End Class
Public Class Result
Public Property success As Boolean
Public Property total As Integer
Public Property records As Record()
End Class
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
Public Property result As Result
End Class
因此,在使用以下语句对第一种情况JSON反序列化时,它可以很好地工作:
Dim myResponse = JsonConvert.DeserializeObject(Of Response)(myJsonString)
但是,当接收第二种情况JSON时,它将创建类型为Response的对象,在其属性结果中存储类型为Result的对象,该对象的属性保持为空,并且我需要为其检索的数据也消失了。
我认为我应该修改Response类,以便通过以下方式为不同的数据集放置位置:
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
Public Property result As Result
Public Property resultRecords As Record()
End Class
那么,问题是:我该如何告诉JsonConvert是将数据存储在适合其类型的属性Response.result中(上面的第一个示例),还是将响应存储在Response.resultRecords中?
谢谢!
由于JSON的格式对于同一属性可能有所不同,因此您将需要一个自定义样式JsonConverter
来处理它。转换器可以读取受影响属性的JSON,确定其格式,然后适当地填充您的对象。这是您需要的代码:
Public Class ResultConverter
Inherits JsonConverter
Public Overrides Function CanConvert(objectType As Type) As Boolean
Return objectType = GetType(Result)
End Function
Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
Dim token As JToken = JToken.Load(reader)
Dim result As Result = New Result
If token.Type = JTokenType.Array Then
result.records = token.ToObject(Of Record())(serializer)
result.total = result.records.Length
result.success = True
Else
serializer.Populate(token.CreateReader(), result)
End If
Return result
End Function
Public Overrides ReadOnly Property CanWrite As Boolean
Get
Return False
End Get
End Property
Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer)
Throw New NotImplementedException
End Sub
End Class
要使用转换器,请在类<JsonConverter>
的result
属性中添加一个属性Response
:
Public Class Response
Public Property type As String
Public Property tid As Integer
Public Property action As String
Public Property method As String
<JsonConverter(GetType(ResultConverter))>
Public Property result As Result
End Class
这是一个有用的小提琴,它表明此转换器将允许将两种JSON格式反序列化为相同的类:https : //dotnetfiddle.net/NFbQ2Q
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句