tryonlinux
tryonlinux

Reputation: 128

Error converting value {null} to type 'System.DateTime' Json Response VB.NET

I am trying to use JsonConvert.DeserializeObject to parse a JSON response that I am getting back from a web-service. I have the XSD Schema file for the service, which I ran though the .net converter tool to make a Class. Which everything works perfectly, other than the below field. I get the error in the title. It only happens on Array's of Dates, single dates that have a null return work perfectly fine and are skipped due to turning on the NullValueHandling.Ignore setting. Does anyone know how I could use JSON.NET to skip over theses null dates in the array too? Thanks in advance!

JSON Response: "TheDatesReturned":[null,null,null,null,null,null],

 Private TheDatesReturnedField() As Date

 <System.Xml.Serialization.XmlElementAttribute("TheDatesReturned", DataType:="date")> _
Public Property TheDatesReturned() As Date()
    Get
        Return Me.TheDatesReturnedField
    End Get
    Set(value As Date())
        Me.TheDatesReturnedField= value
    End Set
End Property

NOTE: Changing it to an array of strings fixes it as well, but then they are no longer typed correctly when I actually do get a response.

EDIT:

If any anyone comes across this and is wondering how to get XSD.exe to do it for them. They can add nillable="true" to the field in the XSD file

<xsd:element maxOccurs="6" minOccurs="0" name="TheDatesReturned" type="xsd:date" nillable="true">

This will then generate the Class with this, which should take care of the issue.

Private TheDatesReturnedField() As System.Nullable(Of Date)   

Upvotes: 1

Views: 3671

Answers (1)

Sam
Sam

Reputation: 10113

Possibly because DateTime is not a nullable type? Try using DateTime? as the type.

Upvotes: 1

Related Questions