首页 > 解决方案 > 使用 Newtonsoft.Json.JsonConvert.DeserializeObject VISUAL BASIC 将 json 字符串反序列化为 Visual Basic 对象

问题描述

我正在使用 Newtonsoft.Json.JsonConvert.DeserializeObject VISUAL BASIC,我需要将 json 字符串反序列化为一个名为“Elemento”的对象。

我的代码:

request.RequestFormat = DataFormat.Json
            Dim json As String = "{""where"":""id==" + oid + """, ""selects"":[""current"", ""name"", ""type"", ""revision"", ""owner"", ""description""]}"
            request.AddParameter("application/json", json, ParameterType.RequestBody)

Dim response As IRestResponse = client.Execute(request)
Dim code As String = response.StatusCode

Dim elemento As Elemento = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Elemento)(response.Content)

Dim tipo As String = elemento.type
Dim current As String = elemento.current
Dim nombre As String = elemento.name
Return elemento

收起的json:


{
    "msg": "OK",
    "data": [
        {
            "name": "LABORATORIO",
            "owner": "aviteri",
            "current": "Active",
            "id": "62676.39210.18680.50051",
            "type": "Workspace",
            "revision": "0000000026"
        }
    ],
    "objectsLabels": {
        "name": "Name",
        "owner": "Owner",
        "current": "Maturity State",
        "id": "emxFramework.Attribute.id",
        "type": "Type",
        "revision": "Revision"
    }
}

我需要将 json 的“数据”信息反序列化到我的对象 Elemento 中,我尝试了最后一个代码,但我收到了空信息。只需忽略 objectsLabels 数据。

这是我的“Elemento”类,它具有相同的属性和更多属性:

Public Class Elemento

#Region "Variables"

    Public Enum Valores
        Nulo
        ParaInformacion
        Rechazado
        RevisadoConComentarios
        RevisadoSinComentarios
    End Enum

    Public Enum Estado
        Privado
        En_Desarrollo
        Emitido
        Completado
        Emitido_Cliente
        Obsoleto
        Active
        Create
    End Enum

    End Enum
    Private _objectID As String
    Private _name As String
    Private _typeName As String
    Private _description As String
    Private _owner As String
    Private _revision As String
    Private _title As String
    Private _originated As String
    Private _modified As String
    Private _estado As Estado
    Private _current As String


#End Region

#Region "Properties"

    Public Property id() As String
        Get
            Return _objectID
        End Get
        Set(ByVal value As String)
            _objectID = value
        End Set
    End Property

    Public Property name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property type() As String
        Get
            Return _typeName
        End Get
        Set(ByVal value As String)
            _typeName = value
        End Set
    End Property

    Public Property description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    Public Property owner() As String
        Get
            Return _owner
        End Get
        Set(ByVal value As String)
            _owner = value
        End Set
    End Property

    Public Property revision() As String
        Get
            Return _revision
        End Get
        Set(ByVal value As String)
            _revision = value
        End Set
    End Property

    Public Property title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    Public Property originated() As String
        Get
            Return _originated
        End Get
        Set(ByVal value As String)
            _originated = value
        End Set
    End Property

    Public Property modified() As String
        Get
            Return _modified
        End Get
        Set(ByVal value As String)
            _modified = value
        End Set
    End Property



    Public Property current() As String
        Get
            Return _estado
        End Get
        Set(ByVal value As String)
            _estado = CType([Enum].Parse(GetType(Estado), value), Estado)
        End Set
    End Property

#End Region

标签: vb.net

解决方案


正如 Jimi 在评论中所说,我错过了 RootObject,这是一个具有 msg 数据和 objectsLabels 属性的类。

我创建了一个名为 ResponseListElements 的类

Public Class ResponseListElements
#Region "Variables"

    Private _msg As String = String.Empty
    Private _data As List(Of Elemento)

#End Region
#Region "Properties"

    Public Property msg() As String
        Get
            Return _msg
        End Get
        Set(ByVal value As String)
            _msg = value
        End Set
    End Property

    Public Property data() As List(Of Elemento)

        Get
            Return _data
        End Get
        Set(ByVal value As List(Of Elemento))
            _data = value
        End Set
    End Property
#End Region

End Class

他们我改变了这一行:

Dim elemento As Elemento = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Elemento)(response.Content)

通过这个:

Dim elemento As ResponseListElements = Newtonsoft.Json.JsonConvert.DeserializeObject(Of ResponseListElements)(response.Content)

推荐阅读