整个周末我一直在和这个人打架。我正在尝试使用Newtonsoft工具解析/反序列化我的JSON。
我能够毫无困难地获得顶级数据(EventID和EventName)。
我一直在搜索示例和其他帖子,以获取有关信息的ROOMS数组的帮助。房间和会议将在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
有人可以帮助我访问这些数据元素吗?
非常感谢任何帮助。
提前谢谢。
杰夫
答案 0 :(得分:0)
<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):
}