VB.NET DataContractJsonSerializer 嵌套对象格式化
VB.NET DataContractJsonSerializer Nested Object Formatting
我对序列化的结果有疑问。我序列化的 class 看起来像这样:
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON
<Runtime.Serialization.DataMember(Name:="success")>
Public Property Success As Boolean
<Runtime.Serialization.DataMember(Name:="message")>
Public Property Message As String
<Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
Public Property Data As Object
Public Sub New()
End Sub
Public Sub New(success As Boolean)
Me.Success = success
End Sub
Public Sub New(message As String)
Me.Message = message
End Sub
Public Sub New(message As String, success As Boolean)
Me.Message = message
Me.Success = success
End Sub
End Class
Data-属性 可以是任何东西,在我的例子中它是一个 List(Of Dictionary(Of String, Object))。
问题:
如果我将列表放入 属性 并将其序列化,结果类似于:
{"success":true,"message":"",data:[[{"key":"name", value:1}, {"key":"foo", value:"bar"}],[{"key":"foo", value:"bar"}]]}
你可以看到 List 中的每个字典都被序列化为 { "key":"foo", "value":"bar" } 的数组,这很奇怪,因为如果我序列化纯列表(不是嵌套在 ResponseJSON class) 中,它将序列化为 { "foo":"bar" } 这是我一直想要的
这些是序列化程序的设置:
mvoSettings = New Runtime.Serialization.Json.DataContractJsonSerializerSettings
mvoSettings.SerializeReadOnlyTypes = True
mvoSettings.MaxItemsInObjectGraph = Integer.MaxValue / 2
mvoSettings.EmitTypeInformation = Runtime.Serialization.EmitTypeInformation.Never
mvoSettings.UseSimpleDictionaryFormat = True
mvoSettings.IgnoreExtensionDataObject = True
mvoSettings.DateTimeFormat = New Runtime.Serialization.DateTimeFormat("dd.MM.yyyy HH:mm:ss")
这是序列化:
Public Function ToJSON(obj As Object) As IO.MemoryStream
Dim serializer As New Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType(), mvoSettings)
Dim stream As New IO.MemoryStream
serializer.WriteObject(stream, obj)
Return stream
End Function
想要的结果:
{"success":true,"message":"",data:[{"foo":"bar", "name":"Jon"},{"Jon":"Doe"}]}
我想我现在知道问题了...我将 Data-属性 的类型从 Object 更改为 List(Of Dictionary(Of String, Object)) 并且它起作用了。我假设序列化程序需要显式声明才能按需要进行序列化。
更改如下:
- 从 ResponseJSON
中删除了数据-属性
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON
<Runtime.Serialization.DataMember(Name:="success")>
Public Property Success As Boolean
<Runtime.Serialization.DataMember(Name:="message")>
Public Property Message As String
End Class
- 使用通用数据创建派生 class-属性
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON(Of T)
Inherits ResponseJSON
<Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
Public Property Data As T
End Class
我对序列化的结果有疑问。我序列化的 class 看起来像这样:
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON
<Runtime.Serialization.DataMember(Name:="success")>
Public Property Success As Boolean
<Runtime.Serialization.DataMember(Name:="message")>
Public Property Message As String
<Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
Public Property Data As Object
Public Sub New()
End Sub
Public Sub New(success As Boolean)
Me.Success = success
End Sub
Public Sub New(message As String)
Me.Message = message
End Sub
Public Sub New(message As String, success As Boolean)
Me.Message = message
Me.Success = success
End Sub
End Class
Data-属性 可以是任何东西,在我的例子中它是一个 List(Of Dictionary(Of String, Object))。
问题: 如果我将列表放入 属性 并将其序列化,结果类似于:
{"success":true,"message":"",data:[[{"key":"name", value:1}, {"key":"foo", value:"bar"}],[{"key":"foo", value:"bar"}]]}
你可以看到 List 中的每个字典都被序列化为 { "key":"foo", "value":"bar" } 的数组,这很奇怪,因为如果我序列化纯列表(不是嵌套在 ResponseJSON class) 中,它将序列化为 { "foo":"bar" } 这是我一直想要的
这些是序列化程序的设置:
mvoSettings = New Runtime.Serialization.Json.DataContractJsonSerializerSettings
mvoSettings.SerializeReadOnlyTypes = True
mvoSettings.MaxItemsInObjectGraph = Integer.MaxValue / 2
mvoSettings.EmitTypeInformation = Runtime.Serialization.EmitTypeInformation.Never
mvoSettings.UseSimpleDictionaryFormat = True
mvoSettings.IgnoreExtensionDataObject = True
mvoSettings.DateTimeFormat = New Runtime.Serialization.DateTimeFormat("dd.MM.yyyy HH:mm:ss")
这是序列化:
Public Function ToJSON(obj As Object) As IO.MemoryStream
Dim serializer As New Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType(), mvoSettings)
Dim stream As New IO.MemoryStream
serializer.WriteObject(stream, obj)
Return stream
End Function
想要的结果:
{"success":true,"message":"",data:[{"foo":"bar", "name":"Jon"},{"Jon":"Doe"}]}
我想我现在知道问题了...我将 Data-属性 的类型从 Object 更改为 List(Of Dictionary(Of String, Object)) 并且它起作用了。我假设序列化程序需要显式声明才能按需要进行序列化。
更改如下:
- 从 ResponseJSON 中删除了数据-属性
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON
<Runtime.Serialization.DataMember(Name:="success")>
Public Property Success As Boolean
<Runtime.Serialization.DataMember(Name:="message")>
Public Property Message As String
End Class
- 使用通用数据创建派生 class-属性
<Runtime.Serialization.DataContract()>
Public Class ResponseJSON(Of T)
Inherits ResponseJSON
<Runtime.Serialization.DataMember(Name:="data", isRequired:=False)>
Public Property Data As T
End Class