1

I get the error "Cannot convert NULL to a value type" I have seen some other answers on ignoring null values, but I cannot seem to find the property in the Javascriptserializer class. (or any of the other serialization classes offered by .NET)

I have the following function, which uses an httpwebrequest to retrieve a JSON string......

  Public Shared Function GetPagerAssignments(Optional ByVal ActiveOnly As Boolean = True) As List(Of PagerAssignment)
    Dim mylist As New List(Of PagerAssignment)

    Dim myrequest As HttpWebRequest = HttpWebRequest.Create("My URL Here")
    myrequest.Proxy = Nothing
    myrequest.UserAgent = "PAGER"
    myrequest.Method = "GET"

    Dim myresponse As HttpWebResponse = myrequest.GetResponse
    Dim mystream As System.IO.Stream = (myresponse.GetResponseStream)

    Dim streamreader As New System.IO.StreamReader(mystream)

    Dim myjsonstring As String = streamreader.ReadToEnd

    Try
        Dim jss = New JavaScriptSerializer()


        mylist = jss.Deserialize(Of List(Of PagerAssignment))(myjsonstring)

    Catch ex As Exception
        MessageBox.Show("ERROR GETTING PAGER ASSIGNMENTS: " & ex.ToString)
    End Try

    Return mylist
end function

I have a date value in my JSON string that returns a NULL value, but I need to use this. Here is my JSON..

[
{
""ID"":""283"",
""FirstName"":""JOHN"",
""LastName"":""DOE"",
""AssignedDate"":{""date"":""2019-01-14 09:35:15.573000"",""timezone_type"":3,""timezone"":""America\/Tegucigalpa""},
""RegisteredDate"":{""date"":""2019-01-14 19:46:43.883000"",""timezone_type"":3,""timezone"":""America\/Tegucigalpa""},
""DoctorID"":""54"",
""RoomNumber"":""999"",
""PagerID"":""14"",
""AssigningUser"":""BILLYBOB"",
""Procedure"":""NONE"",
""Notes"":"""",
""ReturnStatus"":0,
""ReturnedDate"":null,
""IsHeld"":0,
""HasOrientation"":0,
""PagerTypeID"":""1"",
""DoctorName"":""PEPPER, DR"",
""PagerName"":""14"",
""PagerTypeName"":""Family""
},
{""ID"":""297"",
""FirstName"":""BUGS"",
""LastName"":""BUNNY"",
""AssignedDate"":{""date"":""2019-01-14 20:29:17.937000"",""timezone_type"":3,""timezone"":""America\/Tegucigalpa""},
""RegisteredDate"":null,
""DoctorID"":""81"",
""RoomNumber"":""45"",
""PagerID"":""20"",
""AssigningUser"":""HOMER S"",
""Procedure"":""54545"",
""Notes"":"""",
""ReturnStatus"":0,
""ReturnedDate"":null,
""IsHeld"":0,
""HasOrientation"":0,
""PagerTypeID"":""1"",
""DoctorName"":""MONROE, MARVIN"",
""PagerName"":""20"",
""PagerTypeName"":""Family""
},
{""ID"":""295"",
""FirstName"":""DAFFY"",
""LastName"":""DUCK"",
""AssignedDate"":{""date"":""2019-01-14 16:11:06.830000"",""timezone_type"":3,""timezone"":""America\/Tegucigalpa""},
""RegisteredDate"":{""date"":""2019-01-14 19:55:50.290000"",""timezone_type"":3,""timezone"":""America\/Tegucigalpa""},
""DoctorID"":""81"",
""RoomNumber"":""876"",
""PagerID"":""24"",
""AssigningUser"":""BART S"",
""Procedure"":""TEST PROCEDURE"",
""Notes"":"""",
""ReturnStatus"":0,
""ReturnedDate"":null,
""IsHeld"":0,
""HasOrientation"":0,
""MRN"":""8734"",
""PagerTypeID"":""1"",
""DoctorName"":""GOODE, PHIL"",
""PagerName"":""24"",
""PagerTypeName"":""Family""
}

]

0

2 Answers 2

1

This class is not recommended for this use per the docs. Here is an alternative implementation using Json.Net that should work create assuming your class PagerAssignment accepts nulls for RecordData.

    Public Function GetPagerAssignments(Optional ByVal ActiveOnly As Boolean = True) As List(Of PagerAssignment)
    Dim mylist As New List(Of PagerAssignment)

    Dim myrequest As HttpWebRequest = HttpWebRequest.Create("My URL Here")
    myrequest.Proxy = Nothing
    myrequest.UserAgent = "PAGER"
    myrequest.Method = "GET"

    Dim myresponse As HttpWebResponse = myrequest.GetResponse
    Dim mystream As System.IO.Stream = (myresponse.GetResponseStream)

    Dim streamreader As New System.IO.StreamReader(mystream)

    Dim myjsonstring As String = streamreader.ReadToEnd

    Try
        mylist = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of PagerAssignment))(myjsonstring)

    Catch ex As Exception
        MessageBox.Show("ERROR GETTING PAGER ASSIGNMENTS: " & ex.ToString)
    End Try

    Return mylist
End Function

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is some test code I used. It did not throw the exception you are seeing:

Module Module1
Sub Main()
    Dim json = "[{'Id':1 'RecordDate': null},{'Id':2 'RecordDate': null}]"
    GetPagerAssignments(json)
    Console.Read()
End Sub

Public Function GetPagerAssignments(dataAsJson As String) As List(Of PagerAssignment)
    Dim mylist As New List(Of PagerAssignment)


    Try
        Dim jss = New JavaScriptSerializer()


        mylist = jss.Deserialize(Of List(Of PagerAssignment))(dataAsJson)

    Catch ex As Exception
        'MessageBox.Show("ERROR GETTING PAGER ASSIGNMENTS: " & ex.ToString)
    End Try

    Return mylist
End Function
End Module 

And Here is the class I used:

Friend Class PagerAssignment
   Public Id As Int32
   Public RecordDate As DateTime?
End Class

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.