Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET Program - problems deserializing JSON into List of objects

Status
Not open for further replies.

markronz

IS-IT--Management
Mar 20, 2007
93
US
Hello All-
I am hoping someone may be able to help me figure this out. I am querying something via an API call, and it does return a list of items in its RawJSON. Right now my code does that much correctly, but I'm having difficulty turning that into a list of ServerRecord objects. Please see my code below and let me know if you have any tips for me on what I am messing up. Please let me know if you need any more information in order to help me out. Thanks in advance!


Code:
Public Class ServerRecord
        Inherits Record
        <Newtonsoft.Json.JsonProperty("sys_class_name")>
        Public Property sys_class_name As String
            Get
            End Get
            Set(value As String)
            End Set
        End Property


        <Newtonsoft.Json.JsonProperty("host_name")>
        Public Property host_name As String
            Get
            End Get
            Set(value As String)
            End Set
        End Property
        <Newtonsoft.Json.JsonProperty("u_recovery_time_achievable")>
        Public Property u_recovery_time_achievable As String
            Get
            End Get
            Set(value As String)
            End Set
        End Property
    End Class


        Dim lstSNServersList As New List(Of ServerRecord)
        Dim objServiceNowTableAPIClient As TableAPI.TableAPIClient(Of ServerRecord)
        Dim objServiceNowRESTQueryResponse As RESTQueryResponse(Of ServerRecord)

            objServiceNowTableAPIClient = New TableAPIClient(Of wServerRecord)(strServiceNowCMDBServersTableName, strServiceNowInstanceName, strServiceNowUser, strServiceNowPassword)
            strServiceNowQuery = "sys_class_name=cmdb_ci_win_server^ORsys_class_name=cmdb_ci_linux_server"
            objServiceNowRESTQueryResponse = objServiceNowTableAPIClient.GetByQuery(strServiceNowQuery)
            'this much does work and it does return a result set

            [b]'this is my attempt to convert this response into a list of ServerRecords, but this does not work currently[/b]:
            lstSNServersList = JsonConvert.DeserializeObject(Of List(Of ServerRecord))(objServiceNowRESTQueryResponse.RawJSON)

The objServiceNowRestQueryResponse.RawJSON string looks like this (though much longer):
"{""result"":[{""sys_id"":""00040665dbxxxxxx96191e"",""u_recovery_time_achievable"":""720"",""sys_class_name"":""cmdb_ci_linux_server"",""host_name"":""rlserver001""},{""sys_id"":""00ec543d1xxxx66e4bcb6d"",""u_recovery_time_achievable"":""4"",""sys_class_name"":""cmdb_ci_linux_server"",""host_name"":""plserver001""},{""sys_id"":""0105d975dbxxxxx8961998"",""u_recovery_time_achievable"":"""",""sys_class_name"":""cmdb_ci_linux_server"",""host_name"":""tlserver001""},


This is the error message I get when trying to run my code:
Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll
The thread 0x20b4 has exited with code 0 (0x0).
The thread 0x3e24 has exited with code 0 (0x0).
The program '[5892] CMDBReconciliation.vshost.exe' has exited with code 0 (0x0).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top