JSON使用Newtonsoft解析子项目VB.NET



我整个周末都一直在与我战斗。我正在尝试使用newtonsoft工具来解析/应对我的JSON。

我能够在没有麻烦的情况下到达顶级数据(EventID和EventName(。

我一直在搜寻示例和其他帖子,以帮助您在信息中获取信息。房间和会议将在JSON集合中有0个数量的物品。


我的JSON:

{
    "EventID": 123,
    "EventName": "Training Camp",
    "Rooms": [
        {
            "RoomID": 12,
            "RoomName": "Main Ballroom"
        },
        {
            "RoomID": 256,
            "RoomName": "East Hall"
        }
    ],
    "Sessions": [
        {
            "SessName": "Session One",
            "ScheduleID": 1682,
        },
        {
            "SessName": "Session Two",
            "ScheduleID": 1683,
        }
    ]
}

我的代码:

Public Class JSONEvent
    Public EventID As Integer
        Public EventName As String
        Public RoomsArray As List(Of JSONRooms)
End Class
Public Class JSONRooms
    Public Property RoomName As String
    Public Property RoomID As String
End Class

form1:

Dim obj = JsonConvert.DeserializeObject(Of JSONEvent)(JsonData)
messagebox.show(obj.eventid)
messagebox.show(obj.eventname)
Dim TheEvent As JSONEvent = JsonConvert.DeserializeObject(Of JSONEvent)(JsonData)
TheEvent.RoomsArray = JsonConvert.DeserializeObject(Of List(Of JSONRooms))(TheEvent.roomname) 'this I can't get right

有人可以帮助我访问这些数据元素吗?

任何帮助都非常感谢。

预先感谢您。

jeff

    <Serializable> _
    Public Class Room
        Public Property RoomID() As Integer
            Get
                Return m_RoomID
            End Get
            Set
                m_RoomID = Value
            End Set
        End Property
        Private m_RoomID As Integer
        Public Property RoomName() As String
            Get
                Return m_RoomName
            End Get
            Set
                m_RoomName = Value
            End Set
        End Property
        Private m_RoomName As String
    End Class
    <Serializable> _
    Public Class Session
        Public Property SessName() As String
            Get
                Return m_SessName
            End Get
            Set
                m_SessName = Value
            End Set
        End Property
        Private m_SessName As String
        Public Property ScheduleID() As Integer
            Get
                Return m_ScheduleID
            End Get
            Set
                m_ScheduleID = Value
            End Set
        End Property
        Private m_ScheduleID As Integer
    End Class
    <Serializable> _
    Public Class JSONEvent
        Public Property EventID() As String
            Get
                Return m_EventID
            End Get
            Set
                m_EventID = Value
            End Set
        End Property
        Private m_EventID As String
        Public Property EventName() As String
            Get
                Return m_EventName
            End Get
            Set
                m_EventName = Value
            End Set
        End Property
        Private m_EventName As String
        Public Property Rooms() As List(Of Room)
            Get
                Return m_Rooms
            End Get
            Set
                m_Rooms = Value
            End Set
        End Property
        Private m_Rooms As List(Of Room)
        Public Property Sessions() As List(Of Session)
            Get
                Return m_Sessions
            End Get
            Set
                m_Sessions = Value
            End Set
        End Property
        Private m_Sessions As List(Of Session)
    End Class

和c#

[Serializable]
public class Room
{
    public int RoomID { get; set; }
    public string RoomName { get; set; }
}
[Serializable]
public class Session
{
    public string SessName { get; set; }
    public int ScheduleID { get; set; }
}
[Serializable]
public class JSONEvent
{
    public string EventID { get; set; }
    public string EventName { get; set; }
    public List<Room> Rooms { get; set; }
    public List<Session> Sessions { get; set; }
}

    // your code should work except the whole object should be deserialized at once.   

        JSONEvent obj = JsonConvert.DeserializeObject<JSONEvent>(JsonData);
        messagebox.show(obj.EventID);
        messagebox.show(obj.EventName);
        // check the array
        if(obj.Rooms != null && obj.Rooms.Count > 0)
        {
            messagebox.show(obj.Rooms[0].RoomID.ToString());
            messagebox.show(obj.Rooms[0].RoomName):
        }

相关内容

  • 没有找到相关文章

最新更新