Reputation: 6586
I'm trying to deserialize some Json to a .Net class. I'm having a problem with the contacttypeidlist field. How can I change the Json to get this to work?
I am receiving the following error:
Cannot convert object of type 'System.String' to type'System.Collections.Generic.List`1[System.Int32]'
Json
[
{
"fullname": "bob smith",
"email": "[email protected]",
"phone": "555-5555",
"onsite": "true",
"contacttypeidlist": "[1,2,3]"
}
]
Deserialize code
Dim oSerializerSort As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim lobjSiteContactsList = oSerializerSort.Deserialize(Of List(Of SiteContact))(lstrSiteContactsJson)
SiteContact Class Definition
Public Property fullname As String
Public Property email As String
Public Property phone As String
Public Property onsite As Boolean
Public Property contacttypeidlist As List(Of Integer)
Upvotes: 0
Views: 933
Reputation: 1038850
The "contacttypeidlist"
property in your JSON is just a simple plain string. So make sure you respect this in your model:
Public Property contacttypeidlist As String
On the other hand if your JSON looked like this (notice the missing double quotes around the value):
"contacttypeidlist": [1,2,3]
you could have used an integer array in your VB.NET model.
If you have no control over the original JSON and cannot fix its format so that the contacttypeidlist
property represents an actual integer array instead of a string then you have to perform the deserialization in 2 steps: first deserialize the original JSON into a structure with a string property. And in a second step deserialize this string property to an integer array.
Upvotes: 1