VB.net JSON反序列化

我有以下JSON字符串要反序列化

[{"application_id":"1","application_package":"abc"},{"application_id":"2","application_package":"xyz"}]

我正在使用DataContractJsonSerializer方法。

它由项目数组组成,我找不到使用VB.Net可以反序列化此结构的示例。我具有以下Application类来存储此信息:

    <DataContract(Namespace:="")> _

Public Class ApplicationItem

<DataMember(Name:="application_id")>

Public Property application_id As String

<DataMember(Name:="application_package")>

Public Property application_package As String

End Class

回答:

我建议你使用JavaScriptSerializerDataContractJsonSerializer。原因如下:

  • JavaScriptSerializer 更快 DataContractJsonSerializer
  • DataContractJsonSerializerJavaScriptSerializer简单的序列化需要更多的代码。

您无需将DataContractand DataMember属性与JavaScriptSerializer

使用此数据类

<Serializable> _

Public Class ApplicationItem

Public Property application_id() As String

Get

Return m_application_id

End Get

Set

m_application_id = Value

End Set

End Property

Private m_application_id As String

Public Property application_package() As String

Get

Return m_application_package

End Get

Set

m_application_package = Value

End Set

End Property

Private m_application_package As String

End Class

并使用它反序列化您的jsonText

Dim jss As New JavaScriptSerializer()

Dim dict = jss.Deserialize(Of List(Of ApplicationItem))(jsonText)

如果仍要使用DataContractJsonSerializer,则可以使用以下代码反序列化:

Dim obj As New List(Of ApplicationItem)()

Dim ms As New MemoryStream(Encoding.Unicode.GetBytes(json))

Dim serializer As New System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.[GetType]())

obj = DirectCast(serializer.ReadObject(ms), List(Of ApplicationItem))

ms.Close()

ms.Dispose()

礼貌:二手Telerik代码转换器

以上是 VB.net JSON反序列化 的全部内容, 来源链接: utcz.com/qa/421420.html

回到顶部