2017-04-23 95 views
0

我一直在整個週末與這個戰鬥。我試圖用Newtonsoft工具解析/反序列化我的JSON。JSON解析子項VB.net使用Newtonsoft

我能夠毫無問題地達到頂級數據(EventID和EventName)。

我一直在搜索示例和其他帖子,以獲取有關信息的ROOMS數組的幫助。房間和會話將在json集中有0多個項目。


MY 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 

有人可以幫我牛逼o訪問這些數據元素?

任何幫助,非常感謝。

預先感謝您。

傑夫

+0

不要解析它,反序列化 – Plutonix

回答

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): 
     } 
+0

我懶,在C#寫的,然後運行它通過代碼轉換器。 – Bindrid

+0

:)謝謝你的幫助。我將嘗試這種方法。 – Jeffulot

+0

我看到這些類,但是如何訪問數據? – Jeffulot