2016-11-08 154 views
0

解答給出的描述使用LINQ to JSON和JObject將JSON轉換爲動態工作對象。以下是完成的代碼,它將我從JSON轉換爲可用對象,然後是原始問題。VB.NET將Newtonsoft JSON反序列化爲動態對象

  'Parse string of JSON data into a JObject that can be accessed via VB.NET 
      Dim resultSet As JObject = JObject.Parse(responseBody) 

      'Data can be accessed as seen below 
      Dim cpu As String = CType(resultSet("KeyName"), String) 

=========================================== ==============================

我有一個網絡應用程序,將對一個叫做服務的API調用inContact公司(http://www.incontact.com/

每個API調用將被接收填充有以這種方式格式化的JSON HTTP響應:

{ 
    "resultSet": { 
    "_links": { 
     "self": "string", 
     "next": "string", 
     "previous": "string" 
    }, 
    "businessUnitId": 0, 
    "lastPollTime": "2016-11-08T21:45:46.510Z", 
    "totalRecords": 0, 
    "agents": [ 
     { 
     "agentId": 0, 
     "userName": "string", 
     "firstName": "string", 
     "middleName": "string", 
     "lastName": "string", 
     "emailAddress": "string", 
     "isActive": true, 
     "teamId": 0, 
     "teamName": "string", 
     "reportToId": 0, 
     "reportToName": "string", 
     "isSupervisor": true, 
     "lastLogin": "2016-11-08T21:45:46.510Z", 
     "lastUpdated": "2016-11-08T21:45:46.510Z", 
     "location": "string", 
     "custom1": "string", 
     "custom2": "string", 
     "custom3": "string", 
     "custom4": "string", 
     "custom5": "string", 
     "internalId": "string", 
     "profileId": 0, 
     "profileName": "string", 
     "timeZone": "string", 
     "country": "string", 
     "countryName": "string", 
     "state": "string", 
     "city": "string", 
     "chatRefusalTimeout": 0, 
     "phoneRefusalTimeout": 0, 
     "workItemRefusalTimeout": 0, 
     "defaultDialingPattern": 0, 
     "defaultDialingPatternName": "string", 
     "teamDefaultMaxChats": true, 
     "maxConcurrentChats": 0, 
     "notes": "string", 
     "createDate": "2016-11-08T21:45:46.510Z", 
     "inactiveDate": "2016-11-08T21:45:46.510Z", 
     "hireDate": "2016-11-08T21:45:46.510Z", 
     "terminationDate": "2016-11-08T21:45:46.510Z", 
     "rehireStatus": true, 
     "employmentType": 0, 
     "employmentTypeName": "Full-Time", 
     "referral": "string", 
     "atHome": true, 
     "hiringSource": "string", 
     "ntLoginName": "string", 
     "scheduleNotification": "5", 
     "federatedId": "string", 
     "sipUser": "string", 
     "useTeamMaxEmailInboxCount": true, 
     "maxEmailInboxCount": 0 
     } 
    ] 
    } 
} 

我反序列化JSON與Newto nsoft。然而,每個不同的呼叫會有不同的鍵/值對,像這樣的:

{ 
    "resultSet": { 
    "businessUnitId": 0, 
    "lastPollTime": "2016-11-08T21:45:46.604Z", 
    "teams": [ 
     { 
     "teamId": 0, 
     "teamName": "string", 
     "isActive": true, 
     "description": "string", 
     "notes": "string", 
     "lastUpdateTime": "2016-11-08T21:45:46.604Z", 
     "inViewEnabled": true, 
     "wfoEnabled": true, 
     "wfmEnabled": true, 
     "qmEnabled": true, 
     "maxConcurrentChats": 0, 
     "agentCount": 0, 
     "maxEmailInboxCount": true, 
     "inViewGamificationEnabled": true, 
     "inViewChatEnabled": true, 
     "inViewLMSEnabled": true, 
     "analyticsEnabled": true 
     } 
    ], 
    "agents": [ 
     { 
     "agentId": 0, 
     "firstName": "string", 
     "lastName": "string" 
     } 
    ] 
    } 
} 

我目前正在在HTTP響應和反序列化JSON的創建一個可行的.NET對象。要做到這一點,這是我的短代碼,省略了HTTP請求,假設請求成功:

 If Not String.IsNullOrEmpty(responseBody) Then 
      ' Success. Do something with the response. 
      'Declare object for holding the JSON from the API call for this call only (class does not fit other calls) 
      Dim resultSet As GetAgentsAPICall = New GetAgentsAPICall 
      'Deserialize JSON response into the new resultSet object 
      resultSet = JsonConvert.DeserializeObject(responseBody) 

的問題是我需要的,而不是僅僅能夠獲得一個字符串爲每個API調用特定的類,使用JSON格式並將其引入具有與鍵/值匹配的屬性的對象。下面是我有隻發生在上述API調用(第一個上市的,在較長的長度)類:

Public Class GetAgentsAPICall 
    Public Property resultSet As Resultset 
End Class 

Public Class Resultset 
     Public Property _links As _Links 
     Public Property businessUnitId As Integer 
     Public Property lastPollTime As Date 
     Public Property totalRecords As Integer 
     Public Property agents() As Agent 
    End Class 

    Public Class _Links 
     Public Property self As String 
     Public Property _next As String 
     Public Property previous As String 
    End Class 

    Public Class Agent 
     Public Property agentId As Integer 
     Public Property userName As String 
     Public Property firstName As String 
     Public Property middleName As String 
     Public Property lastName As String 
     Public Property emailAddress As String 
     Public Property isActive As Boolean 
     Public Property teamId As Integer 
     Public Property teamName As String 
     Public Property reportToId As Integer 
     Public Property reportToName As String 
     Public Property isSupervisor As Boolean 
     Public Property lastLogin As Date 
     Public Property lastUpdated As Date 
     Public Property location As String 
     Public Property custom1 As String 
     Public Property custom2 As String 
     Public Property custom3 As String 
     Public Property custom4 As String 
     Public Property custom5 As String 
     Public Property internalId As String 
     Public Property profileId As Integer 
     Public Property profileName As String 
     Public Property timeZone As String 
     Public Property country As String 
     Public Property countryName As String 
     Public Property state As String 
     Public Property city As String 
     Public Property chatRefusalTimeout As Integer 
     Public Property phoneRefusalTimeout As Integer 
     Public Property workItemRefusalTimeout As Integer 
     Public Property defaultDialingPattern As Integer 
     Public Property defaultDialingPatternName As String 
     Public Property teamDefaultMaxChats As Boolean 
     Public Property maxConcurrentChats As Integer 
     Public Property notes As String 
     Public Property createDate As Date 
     Public Property inactiveDate As Date 
     Public Property hireDate As Date 
     Public Property terminationDate As Date 
     Public Property rehireStatus As Boolean 
     Public Property employmentType As Integer 
     Public Property employmentTypeName As String 
     Public Property referral As String 
     Public Property atHome As Boolean 
     Public Property hiringSource As String 
     Public Property ntLoginName As String 
     Public Property scheduleNotification As String 
     Public Property federatedId As String 
     Public Property sipUser As String 
     Public Property useTeamMaxEmailInboxCount As Boolean 
     Public Property maxEmailInboxCount As Integer 
End Class 

我試圖避免20或30類似的長篇類預建的,而是建動態修改列表或對象。我需要將這些值存儲在數據庫中,或者修改它們並使用另一個API調用將值返回。有沒有人有最佳做法,還是我堅持20-30大類定義?

謝謝你的時間!

回答

1

解析其與JObject,這也可以使用LINQ查詢(見Newtonsoft的LINQ to JSON API,其中Newtonsoft.Json.Linq命名空間下坐):

JObject o = JObject.Parse(@"{ 
    'CPU': 'Intel', 
    'Drives': [ 
     'DVD read/writer', 
     '500 gigabyte hard drive' 
    ] 
}"); 

string cpu = (string)o["CPU"]; 
// Intel 

string firstDrive = (string)o["Drives"][0]; 
// DVD read/writer 

IList<string> allDrives = o["Drives"].Select(t => (string)t).ToList(); 
// DVD read/writer 
// 500 gigabyte hard drive 

的例子是在C#中,但你可以找到相關的VB。 NET在StackOverflow上的答案(例如,看看the answer here)。

+0

正是我所需要的,一種將其解析爲一個可以被訪問和操縱的廣義對象的方法。謝謝trashr0x –

+0

很高興我能幫到你。 – trashr0x

相關問題