gpt4 book ai didi

json - VB.net JSON 反序列化

转载 作者:行者123 更新时间:2023-12-03 00:00:05 25 4
gpt4 key购买 nike

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

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

我正在使用 DataContractJsonSerializer 方法。

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

    <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

最佳答案

以下是将 JSON 反序列化为对象的最简单方法(使用 .NET 4):

示例 JSON:

{
"dogs":[],
"chickens":[
{
"name":"Macey",
"eggs":7
},
{
"name":"Alfred",
"eggs":2
}
]
}

VB.NET:

Try
Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(JSONString)
Dim a = j("dogs") ' returns empty Object() array
Dim b = j("chickens")(0) ' returns Dictionary(Of String, Object)
Dim c = j("chickens")(0)("name") ' returns String "Macey"
Dim d = j("chickens")(1)("eggs") ' returns Integer 2
Catch ex As Exception
' in case the structure of the object is not what we expected.
End Try

关于json - VB.net JSON 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com