使用 VB.NET 进行动态 JSON 反序列化
Dynamic JSON Deserialization with VB.NET
我在 JSON 中收到 API 响应,格式如下
{
"stuffs": {
"prop1": "abcd",
"prop2": "efgh",
"prop3": [
{
"content": "123456",
"name": "abc def",
"id": "123"
},
{
"content": "789012",
"name": "def ghi",
"id": "456"
}
]
}
}
我已经创建了我的 Class 来反序列化这个对象,如下所示
Public Class Prop3
Public Property content As String
Public Property name As String
Public Property id As String
End Class
Public Class Stuffs
Public Property prop1 As String
Public Property prop2 As String
Public Property prop3 As Prop3()
End Class
Public Class Example
Public Property stuffs As Stuffs
End Class
现在的问题是,有时响应不是 prop3 的数组,看起来像这样
{
"stuffs": {
"prop1": "abcd",
"prop2": "efgh",
"prop3": {
"content": "123456",
"name": "abc def",
"id": "123"
}
}
}
这一次我在反序列化时出错,因为 prop3 不再是数组,但是在我的 class 内部声明的 属性 需要一个 属性 prop3 的数组。
有什么动态的方法可以解决这个问题吗?就像 JSON 响应是一个数组一样,它将被视为数组
Public Property prop3 As Prop3()
如果它不是数组,那么它将被视为单个 属性
Public Property prop3 As Prop3
请建议如何克服这个问题,因为更改 API 的响应是不可行的,因为它是由客户开发的,我需要在我的应用程序中使用它,所以我需要找到一些动态的方法来解决这个问题。
根据Craig的建议,我尝试了下面的方法,还是不行,有什么不对的请指教
Public Class SingleValueArrayConverter(Of T)
Inherits JsonConverter
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal value As Object, ByVal serializer As JsonSerializer)
serializer.Serialize(writer, value)
End Sub
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal objectType As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
Dim retVal As Object = New Object()
If reader.TokenType = JsonToken.StartObject Then
Dim instance As T = CType(serializer.Deserialize(reader, GetType(T)), T)
retVal = New List(Of T)() From {
instance
}
ElseIf reader.TokenType = JsonToken.StartArray Then
retVal = serializer.Deserialize(reader, objectType)
End If
Return retVal
End Function
Public Overrides Function CanConvert(ByVal objectType As Type) As Boolean
Return True
End Function
End Class
Public Class myClass1
Public Class Prop3
Public Property content As String
Public Property name As String
Public Property id As String
End Class
Public Class Stuffs
Public Property prop1 As String
Public Property prop2 As String
<JsonProperty("prop3")>
<JsonConverter(GetType(SingleValueArrayConverter(Of Prop3)))>
Public Property prop3 As Prop3()
End Class
Public Class Example
Public Property stuffs As Stuffs
End Class
End Class
报错:
无法将类型 'SingleValueArrayConverter`1[myClass1.myClass+Prop3]' 的对象转换为类型 'Newtonsoft.Json.JsonConverter'。'
请突出显示我遗漏的内容。
修改后有效
Public Property prop3 As Prop3()
至
Public Property prop3 As List(Of Prop3)
我在 JSON 中收到 API 响应,格式如下
{
"stuffs": {
"prop1": "abcd",
"prop2": "efgh",
"prop3": [
{
"content": "123456",
"name": "abc def",
"id": "123"
},
{
"content": "789012",
"name": "def ghi",
"id": "456"
}
]
}
}
我已经创建了我的 Class 来反序列化这个对象,如下所示
Public Class Prop3
Public Property content As String
Public Property name As String
Public Property id As String
End Class
Public Class Stuffs
Public Property prop1 As String
Public Property prop2 As String
Public Property prop3 As Prop3()
End Class
Public Class Example
Public Property stuffs As Stuffs
End Class
现在的问题是,有时响应不是 prop3 的数组,看起来像这样
{
"stuffs": {
"prop1": "abcd",
"prop2": "efgh",
"prop3": {
"content": "123456",
"name": "abc def",
"id": "123"
}
}
}
这一次我在反序列化时出错,因为 prop3 不再是数组,但是在我的 class 内部声明的 属性 需要一个 属性 prop3 的数组。
有什么动态的方法可以解决这个问题吗?就像 JSON 响应是一个数组一样,它将被视为数组
Public Property prop3 As Prop3()
如果它不是数组,那么它将被视为单个 属性
Public Property prop3 As Prop3
请建议如何克服这个问题,因为更改 API 的响应是不可行的,因为它是由客户开发的,我需要在我的应用程序中使用它,所以我需要找到一些动态的方法来解决这个问题。
根据Craig的建议,我尝试了下面的方法,还是不行,有什么不对的请指教
Public Class SingleValueArrayConverter(Of T)
Inherits JsonConverter
Public Overrides Sub WriteJson(ByVal writer As JsonWriter, ByVal value As Object, ByVal serializer As JsonSerializer)
serializer.Serialize(writer, value)
End Sub
Public Overrides Function ReadJson(ByVal reader As JsonReader, ByVal objectType As Type, ByVal existingValue As Object, ByVal serializer As JsonSerializer) As Object
Dim retVal As Object = New Object()
If reader.TokenType = JsonToken.StartObject Then
Dim instance As T = CType(serializer.Deserialize(reader, GetType(T)), T)
retVal = New List(Of T)() From {
instance
}
ElseIf reader.TokenType = JsonToken.StartArray Then
retVal = serializer.Deserialize(reader, objectType)
End If
Return retVal
End Function
Public Overrides Function CanConvert(ByVal objectType As Type) As Boolean
Return True
End Function
End Class
Public Class myClass1
Public Class Prop3
Public Property content As String
Public Property name As String
Public Property id As String
End Class
Public Class Stuffs
Public Property prop1 As String
Public Property prop2 As String
<JsonProperty("prop3")>
<JsonConverter(GetType(SingleValueArrayConverter(Of Prop3)))>
Public Property prop3 As Prop3()
End Class
Public Class Example
Public Property stuffs As Stuffs
End Class
End Class
报错: 无法将类型 'SingleValueArrayConverter`1[myClass1.myClass+Prop3]' 的对象转换为类型 'Newtonsoft.Json.JsonConverter'。'
请突出显示我遗漏的内容。
修改后有效
Public Property prop3 As Prop3()
至
Public Property prop3 As List(Of Prop3)