2016-05-31 41 views
0

嘿所有我有輸出下面的下面的SQL查詢:SQL數據定製JSON.net結構

ID | Name | theRole | THrs | Mon |....| trainH1 | train58 | train52 | train05 |etc... 
=========================================================================================== 
152 | BOB M. | Admin | 40 | 1-9pm |....| 168dGrc0 | 89220E | 2FDEx56 | 5569CCz |...... 

理想我想的結構是這樣的[注:下面附加的表名]:

{ 
    "scheduleName": "", 
    "firstName": "", 
    "lastName": "", 
    "theRole": "", 
    "linker": "", 
    "Schedule": { 
     "ID": "", 
     "totalHrs": "", 
     "Mon": "", 
     "Tue": "", 
     "Wed": "", 
     "Thu": "", 
     "Fri": "", 
     "Sat": "" 
    }, 
    "empInfo": { 
     "ID": "", 
     "Email": "", 
     "Phone": "", 
     "Active": "", 
     "Img": "", 
     "Badge": "" 
    }, 
    "availability": { 
     "ID": "", 
     "Mon": "", 
     "Tue": "", 
     "Wed": "", 
     "Thu": "", 
     "Fri": "", 
     "Sat": "" 
    }, 
    "training": { 
     "?": "?" 
    } 
} 

火車就是我遇到的問題。這些來自數據庫的值可以在任何給定的時間更改名稱或者添加或刪除。目前我有大約35個值。因此,我無法硬編碼這些價值觀我類功能內,我可以ID名稱theRoleTHRS等做...

的VB。淨類定義看起來像這樣到目前爲止

Public Class Schedule 
    Private m_ID As String 
    Private m_totalHrs As String 
    Private m_Mon As String 
    Private m_Tue As String 
    Private m_Wed As String 
    Private m_Thu As String 
    Private m_Fri As String 
    Private m_Sat As String 

    Public Property ID() As String 
     Get 
      Return m_ID 
     End Get 
     Set 
      m_ID = Value 
     End Set 
    End Property 

    Public Property totalHrs() As String 
     Get 
      Return m_totalHrs 
     End Get 
     Set 
      m_totalHrs = Value 
     End Set 
    End Property 

    Public Property Mon() As String 
     Get 
      Return m_Mon 
     End Get 
     Set 
      m_Mon = Value 
     End Set 
    End Property 

    Public Property Tue() As String 
     Get 
      Return m_Tue 
     End Get 
     Set 
      m_Tue = Value 
     End Set 
    End Property 

    Public Property Wed() As String 
     Get 
      Return m_Wed 
     End Get 
     Set 
      m_Wed = Value 
     End Set 
    End Property 

    Public Property Thu() As String 
     Get 
      Return m_Thu 
     End Get 
     Set 
      m_Thu = Value 
     End Set 
    End Property 

    Public Property Fri() As String 
     Get 
      Return m_Fri 
     End Get 
     Set 
      m_Fri = Value 
     End Set 
    End Property 

    Public Property Sat() As String 
     Get 
      Return m_Sat 
     End Get 
     Set 
      m_Sat = Value 
     End Set 
    End Property 
End Class 

Public Class EmpInfo 
    Private m_ID As String 
    Private m_Email As String 
    Private m_Phone As String 
    Private m_Active As String 
    Private m_Img As String 
    Private m_Badge As String 

    Public Property ID() As String 
     Get 
      Return m_ID 
     End Get 
     Set 
      m_ID = Value 
     End Set 
    End Property 

    Public Property Email() As String 
     Get 
      Return m_Email 
     End Get 
     Set 
      m_Email = Value 
     End Set 
    End Property 

    Public Property Phone() As String 
     Get 
      Return m_Phone 
     End Get 
     Set 
      m_Phone = Value 
     End Set 
    End Property 

    Public Property Active() As String 
     Get 
      Return m_Active 
     End Get 
     Set 
      m_Active = Value 
     End Set 
    End Property 

    Public Property Img() As String 
     Get 
      Return m_Img 
     End Get 
     Set 
      m_Img = Value 
     End Set 
    End Property 

    Public Property Badge() As String 
     Get 
      Return m_Badge 
     End Get 
     Set 
      m_Badge = Value 
     End Set 
    End Property 
End Class 

Public Class Availability 
    Private m_ID As String 
    Private m_Mon As String 
    Private m_Tue As String 
    Private m_Wed As String 
    Private m_Thu As String 
    Private m_Fri As String 
    Private m_Sat As String 

    Public Property ID() As String 
     Get 
      Return m_ID 
     End Get 
     Set 
      m_ID = Value 
     End Set 
    End Property 

    Public Property Mon() As String 
     Get 
      Return m_Mon 
     End Get 
     Set 
      m_Mon = Value 
     End Set 
    End Property 

    Public Property Tue() As String 
     Get 
      Return m_Tue 
     End Get 
     Set 
      m_Tue = Value 
     End Set 
    End Property 

    Public Property Wed() As String 
     Get 
      Return m_Wed 
     End Get 
     Set 
      m_Wed = Value 
     End Set 
    End Property 

    Public Property Thu() As String 
     Get 
      Return m_Thu 
     End Get 
     Set 
      m_Thu = Value 
     End Set 
    End Property 

    Public Property Fri() As String 
     Get 
      Return m_Fri 
     End Get 
     Set 
      m_Fri = Value 
     End Set 
    End Property 

    Public Property Sat() As String 
     Get 
      Return m_Sat 
     End Get 
     Set 
      m_Sat = Value 
     End Set 
    End Property 
End Class 

Public Class Training 
    Private m_something1 As String 
    Private m_something2 As String 
    Private m_something3 As String 

    Public Property something1() As String 
     Get 
      Return m_something1 
     End Get 
     Set 
      m_something1 = Value 
     End Set 
    End Property 

    Public Property something2() As String 
     Get 
      Return m_something2 
     End Get 
     Set 
      m_something2 = Value 
     End Set 
    End Property 

    Public Property something3() As String 
     Get 
      Return m_something3 
     End Get 
     Set 
      m_something3 = Value 
     End Set 
    End Property 
End Class 

Public Class RootObject 
    Private m_scheduleName As String 
    Private m_firstName As String 
    Private m_lastName As String 
    Private m_theRole As String 
    Private m_linker As String 
    Private m_Schedule As Schedule 
    Private m_empInfo As EmpInfo 
    Private m_availability As Availability 
    Private m_training As Training 

    Public Property scheduleName() As String 
     Get 
      Return m_scheduleName 
     End Get 
     Set 
      m_scheduleName = Value 
     End Set 
    End Property 

    Public Property firstName() As String 
     Get 
      Return m_firstName 
     End Get 
     Set 
      m_firstName = Value 
     End Set 
    End Property 

    Public Property lastName() As String 
     Get 
      Return m_lastName 
     End Get 
     Set 
      m_lastName = Value 
     End Set 
    End Property 

    Public Property theRole() As String 
     Get 
      Return m_theRole 
     End Get 
     Set 
      m_theRole = Value 
     End Set 
    End Property 

    Public Property linker() As String 
     Get 
      Return m_linker 
     End Get 
     Set 
      m_linker = Value 
     End Set 
    End Property 

    Public Property Schedule() As Schedule 
     Get 
      Return m_Schedule 
     End Get 
     Set 
      m_Schedule = Value 
     End Set 
    End Property 

    Public Property empInfo() As EmpInfo 
     Get 
      Return m_empInfo 
     End Get 
     Set 
      m_empInfo = Value 
     End Set 
    End Property 

    Public Property availability() As Availability 
     Get 
      Return m_availability 
     End Get 
     Set 
      m_availability = Value 
     End Set 
    End Property 

    Public Property training() As Training 
     Get 
      Return m_training 
     End Get 
     Set 
      m_training = Value 
     End Set 
    End Property 
End Class 

我怎麼能當我不知道數據名稱是什麼時,爲json.net序列化創建一個結構?我正在考慮一種類型的列表(字符串)甚至字典(字符串,字符串)但仍然不確定如何去添加這些類,並使用這些值的SQL數據填充它。

所以這是我問:

(1)我怎樣才能獲得階級結構接受SQL列名和值的時候,我不知道這些表的名稱是什麼? (2)能夠形成json結構,因爲我想從類函數中獲取數據。

+0

如何你現在在讀的SQL數據爲你做知道的名字其他值? –

+0

只需使用常規的SQLReader循環。 – StealthRT

回答

1

本來我以爲Dictionary(Of String, String)是完美的,但從您的意見中可以看出,培訓項目可能有重複的列名。正因爲如此,字典在這裏不起作用,而且,你想要的JSON格式也不行。您需要將JSON的training部分更改爲一個對象數組,以便處理潛在的重複。

以下是我在想你可以把它的工作:

  1. 更改Training類有兩個公共屬性,NameValue

  2. 在您的RootObject類中,將training屬性定義爲List(Of Training)而不是單個Training

    Public Class RootObject 
        ... 
        Private m_training As List(Of Training) 
    
        ... 
        Public Property training() As List(Of Training) 
         Get 
          If m_training Is Nothing Then 
           m_training = New List(Of Training)) 
          End If 
          Return m_training 
         End Get 
         Set(value As List(Of Training)) 
          m_training = value 
         End Set 
        End Property 
    End Class 
    
  3. 在您的SQL查詢,確保所有培訓相關的列來最後SELECT子句。既然你說列名不共用一個共同的前綴,他們只能看到識別他們的方式是按位置。

    SELECT ID, Name, theRole, THrs, Mon, ..., EMPTR.* 
    FROM ... 
    
  4. 在你的讀者循環,填充所有已知屬性的行之後,你可以循環遍歷讀者其餘字段以填充該行培訓項目的列表。

    Const firstTrainingColumnIndex As Integer = 5 'Adjust as necessary for your actual data ' 
    
    Dim i As Integer 
    For i = firstTrainingColumnIndex To reader.FieldCount - 1 
        trainingItem.Name = reader.GetName(i) 
        trainingItem.Value = IIf(reader.IsDBNull(i), Nothing, reader.GetString(i)) 
        rootObj.training.Add(trainingItem) 
    Next 
    
  5. 要創建JSON輸出,只是像序列化你通常會。 Json.Net已經知道如何處理List

    Dim json as String = JsonConvert.SerializeObject(rootObj, Formatting.Indented) 
    

    輸出應該結束這樣看:

    { 
        "scheduleName": "Bob M", 
    
        ... 
    
        "training": [ 
        { 
         "Name": "trainH1", 
         "Value": "168dGrc0" 
        }, 
        { 
         "Name": "train58", 
         "Value": "89220E" 
        }, 
        { 
         "Name": "train52", 
         "Value": "2FDEx56" 
        }, 
        { 
         "Name": "train05", 
         "Value": "5569CCz" 
        } 
        ] 
    } 
    
+0

那裏有一個很好的例子,Brian!但是,訓練部分在所有的行中都不會有「火車」這個詞,所以我不能僅僅通過找到這個詞去。有沒有辦法獲得行名稱作爲其循環? – StealthRT

+0

由於這是我要求獲取所有數據的查詢的一部分** [dbo]。[training]作爲EMPTR **。所以我打電話給它** EMPTR **並在我的* select *我有** EMPTR。***。是否有可能獲得開始** EMPTR + columnName **? – StealthRT

+0

閱讀者看到的列名稱是他們在SQL查詢中的內容。所以如果你選擇了train58作爲EMPTR,那麼讀者將會看到'EMPTR'而不是'train58'。如果EMPTR是表(而不是列)的別名,那麼否,讀者不會將表別名視爲列名的一部分。要使用上述方法,您需要一些常用方法來識別與培訓相關的列。如果你不能用名字來做,你可以按位置來做嗎?換句話說,你是否可以在所有知名專欄之後製作所有可變培訓專欄? (續) –