System.Web.Script.Serialization.JavaScriptSerializer反序列化方法在Null值上引发错误

时间:2019-01-15 03:36:11

标签: .net json vb.net javascriptserializer

我收到错误消息“无法将NULL转换为值类型”,我已经看到了一些其他有关忽略空值的答案,但似乎无法在Javascriptserializer类中找到该属性。 (或.NET提供的任何其他序列化类)

我具有以下功能,该功能使用httpwebrequest检索JSON字符串...

  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

我的JSON字符串中有一个日期值,该日期值返回NULL值,但是我需要使用它。这是我的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""
}

]

2 个答案:

答案 0 :(得分:1)

根据docs,不建议将此类用于此用途。这是一个使用Json.Net的替代实现,它可以在您的类PagerAssignment接受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

希望有帮助。

答案 1 :(得分:0)

这是我使用的一些测试代码。它没有引发您看到的异常:

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 

这是我使用的课程:

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