VB.net JSON反序列化 [英] VB.net JSON Deserialize
问题描述
我有以下要反序列化的JSON字符串:
I've got the following JSON string to deserialize:
[{"application_id":"1","application_package":"abc"},{"application_id":"2","application_package":"xyz"}]
我正在使用DataContractJsonSerializer方法.
I'm using DataContractJsonSerializer method.
它由项目数组组成,我找不到使用VB.Net可以反序列化此结构的示例.我具有以下Application类来存储此信息:
It is made up of array of items and I couldn't find an example using VB.Net that can deserialize this structure. I have the following Application class to store this information:
<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
推荐答案
我建议您在DataContractJsonSerializer
上使用JavaScriptSerializer
.原因是:
I'd recommend you to use JavaScriptSerializer
over DataContractJsonSerializer
. The reasons are:
-
JavaScriptSerializer
比DataContractJsonSerializer
更快
-
DataContractJsonSerializer
需要比JavaScriptSerializer
更多的代码才能进行简单的序列化.
JavaScriptSerializer
is faster overDataContractJsonSerializer
DataContractJsonSerializer
requires more code thanJavaScriptSerializer
for a simple serialization.
您不需要DataContract
和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
:
And use this to deserialize your jsonText
:
Dim jss As New JavaScriptSerializer()
Dim dict = jss.Deserialize(Of List(Of ApplicationItem))(jsonText)
如果您仍然想使用DataContractJsonSerializer
,则可以使用以下代码反序列化:
If you still want to use DataContractJsonSerializer
, you can use this code below to deserialize:
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反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!