JSON.NET 在没有成员名称可用的情况下进行反序列化



在 VB.NET 表单应用程序中使用 JSON.NET,我正在使用来自第三方的数据,下面简化了JSON

{
"Venue": {
    "ID": "ABDDF",
    "Name": "My Place",
    "Address": {
        "Address1": "London Road,",
        "Address2": "",
        "Town": "TEST TOWN",
        "County": "TEST",
        "Postcode": "PO1 1ST",
        "Country": "United Kingdom",
        "Region": "Europe"
    },
    "Directions": "Blah Blah Blah",
    "Images": {
        "/images/venueXXX/ABDDF/image1.jpg": "Image of Venue",
        "/images/venueXXX/ABDDF/image10.jpg": "Image of Venue",
        "/images/venueXXX/ABDDF/image2.jpg": "Image of Venue",
        "/images/venueXXX/ABDDF/image3.jpg": "Image of Venue",
        "/images/venueXXX/ABDDF/image4.jpg": "Image of Venue"
    },
    "Equipment": [
        {
            "ID": 8127,
            "VenueID": 1000,
            "EquipmentID": 12,
            "EquipmentName": "Secretarial",
            "MaxQuantity": 1,
            "ShowOnVenuePage": true
        },
        {
            "ID": 8128,
            "VenueID": 1000,
            "EquipmentID": 21,
            "EquipmentName": "Typing Services",
            "MaxQuantity": 1,
            "ShowOnVenuePage": true
        }
    ]
},
"ResponseStatus": {
    "ErrorCode": "200",
    "Message": "OK"
}
}

一切都很顺利,因为我创建了要填充 JSON 数据的类,如下所示。

我调用 JSON.net 的 JsonConvert 来填充类

Dim QueryResult = JsonConvert.DeserializeObject(Of Container)(responseFromServer)

但是,我现在需要引用 JSON 中显示的图像,虽然设备可以在我的类中检索设备,但我无法理解如何让 JSON.Net 访问图像,因为它们不在我所说的命名对中。

所以问题是,我的类声明应该是什么样子才能允许 JSON.net 填充图像(或者我需要添加一个? 另外,供我参考,图像如何存储在此 JSON 中的正确术语是什么?

Public Class VenueDirectoryResult
    Public Response As ResponseStatus
    Public ID As String
    Public Name As String
    Public Address As Address
    Public Directions As String
    Public Equipment As New List(Of Equipment)
End Class
Public Class Address
Public Property Address1() As String
    Get
        Return m_Address1
    End Get
    Set(value As String)
        m_Address1 = value
    End Set
End Property
Private m_Address1 As String
Public Property Address2() As String
    Get
        Return m_Address2
    End Get
    Set(value As String)
        m_Address2 = value
    End Set
End Property
Private m_Address2 As String
Public Property Town() As String
    Get
        Return m_Town
    End Get
    Set(value As String)
        m_Town = value
    End Set
End Property
Private m_Town As String
Public Property County() As String
    Get
        Return m_County
    End Get
    Set(value As String)
        m_County = value
    End Set
End Property
Private m_County As String
Public Property Postcode() As String
    Get
        Return m_Postcode
    End Get
    Set(value As String)
        m_Postcode = value
    End Set
End Property
Private m_Postcode As String
Public Property Country() As String
    Get
        Return m_Country
    End Get
    Set(value As String)
        m_Country = value
    End Set
End Property
Private m_Country As String
Public Property Region() As String
    Get
        Return m_Region
    End Get
    Set(value As String)
        m_Region = value
    End Set
End Property
Private m_Region As String
End Class
Public Class Equipment
Public Property ID() As Integer
    Get
        Return m_ID
    End Get
    Set(value As Integer)
        m_ID = value
    End Set
End Property
Private m_ID As Integer
Public Property VenueID() As Integer
    Get
        Return m_VenueID
    End Get
    Set(value As Integer)
        m_VenueID = value
    End Set
End Property
Private m_VenueID As Integer
Public Property EquipmentID() As Integer
    Get
        Return m_EquipmentID
    End Get
    Set(value As Integer)
        m_EquipmentID = value
    End Set
End Property
Private m_EquipmentID As Integer
Public Property EquipmentName() As String
    Get
        Return m_EquipmentName
    End Get
    Set(value As String)
        m_EquipmentName = value
    End Set
End Property
Private m_EquipmentName As String
Public Property MaxQuantity() As Integer
    Get
        Return m_MaxQuantity
    End Get
    Set(value As Integer)
        m_MaxQuantity = value
    End Set
End Property
Private m_MaxQuantity As Integer
Public Property ShowOnVenuePage() As Boolean
    Get
        Return m_ShowOnVenuePage
    End Get
    Set(value As Boolean)
        m_ShowOnVenuePage = value
    End Set
End Property
Private m_ShowOnVenuePage As Boolean
Public Property Price() As System.Nullable(Of Double)
    Get
        Return m_Price
    End Get
    Set(value As System.Nullable(Of Double))
        m_Price = value
    End Set
End Property
Private m_Price As System.Nullable(Of Double)
Public Property Vatable() As System.Nullable(Of Boolean)
    Get
        Return m_Vatable
    End Get
    Set(value As System.Nullable(Of Boolean))
        m_Vatable = value
    End Set
End Property
Private m_Vatable As System.Nullable(Of Boolean)
End Class

添加了响应类

Public Class ResponseStatus
Public Property ErrorCode() As String
    Get
        Return m_ErrorCode
    End Get
    Set(value As String)
        m_ErrorCode = value
    End Set
End Property
Private m_ErrorCode As String
Public Property Message() As String
    Get
        Return m_Message
    End Get
    Set(value As String)
        m_Message = value
    End Set
End Property
Private m_Message As String
End Class

这是容器类

Public Class Container
    Public Venue As VenueDirectoryResult
End Class

若要处理图像,可以将以下内容添加到VenueDirectoryResult类中:

Public Images As Dictionary(Of String, String)

您可以通过检查字典中的Keys集合来获取图像列表。

就术语而言,图像存储在JSON对象中。 此处使用对象的属性名称来保存图像 URL。

相关内容

  • 没有找到相关文章

最新更新