VB.net JSON 反序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8118019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:03:15  来源:igfitidea点击:

VB.net JSON Deserialize

jsonvb.netdeserialization

提问by Bih Cheng

I've got the following JSON string to deserialize:

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

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

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

I'm using DataContractJsonSerializer method.

我正在使用 DataContractJsonSerializer 方法。

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:

它由项目数组组成,我找不到可以反序列化此结构的使用 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

回答by CrazyTim

Here is the easiest way to deserialize JSON into an object (using .NET 4):

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

Example JSON:

示例 JSON:

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

VB.NET:

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

回答by Abdul Munim

I'd recommend you to use JavaScriptSerializerover DataContractJsonSerializer. The reasons are:

我建议你使用JavaScriptSerializerover DataContractJsonSerializer。原因是:

  • JavaScriptSerializeris faster over DataContractJsonSerializer
  • DataContractJsonSerializerrequires more code than JavaScriptSerializerfor a simple serialization.
  • JavaScriptSerializer更快结束 DataContractJsonSerializer
  • DataContractJsonSerializer需要比JavaScriptSerializer简单序列化更多的代码。

You won't need the DataContractand DataMemberattribute to use along with JavaScriptSerializer

您不需要DataContractDataMember属性一起使用JavaScriptSerializer

Use this data class

使用这个数据类

<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

And use this to deserialize your jsonText:

并使用它来反序列化您的jsonText

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

If you still want to use DataContractJsonSerializer, you can use this code below to deserialize:

如果您仍然想使用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()

Courtesy: Used Telerik Code Converter

礼貌:使用Telerik 代码转换器

回答by Prescott Chartier

This works for me:

这对我有用:

// Get the HttpWebRequest reaponse
string Response = loResponseStream.ReadToEnd();

var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, dynamic>>(Response);
string carrier = (dict["Response"]["carrier"]);