vb.net - 使用 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
解决方案
正如 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)
推荐阅读
- python - 如何将 numpy.ndarray 的索引重置为所需的索引范围?
- c# - Try-Catch 与 If-Else | 我应该争取在这个中使用 If-Else 还是只使用 Try-Catch?
- c# - 从 Messenger Webview 和 Botframework V4 发送和检索数据
- javascript - 允许特定 IP 地址访问我网站上的页面
- angularjs - TypeError: ($scope.values || []).map 不是函数
- mysql - Mysql Join查询与日期分组
- shell - 如何判断我的 C 程序是否是通过 shebang 调用的?
- javascript - After Effects 脚本手动工作,但在启动时不工作
- php - MySQL 选择根据表中的条目数创建排名...怎么做?
- matlab - 使用网格网格查找速度值时如何绘制二维平流问题?