I'm working with an API that returns JSON
. All of the serializing and deserializing has been straightforward so far using the JavaScriptSerializer
class from System.Web.Script.Serialization
. There is an an endpoint that can return errors and doesn't use KVP in its errors list (not sure why, violates their structure for the rest of the api).
I'm having trouble figuring out the class structure to deserialize it.
Example return:
{
"status": "failed",
"errors": [
[
["base", "error details 1"],
["base", "error details 2"],
["base", "error details 3"]
]
]
}
This is a confusing data structure since everything else is paired. But anyway, I've tried using arrays and lists for the errors piece. Here are my classes:
<Serializable> _
Public Class SearchResult
Public Property status As String
Public Property id As Integer
Public Property errors As List(Of APIError)
Public Sub New()
errors = New List(Of APIError)
End Sub
Public Shared Function Deserialize(ByVal json_string As String) As SearchResult
Dim result As SearchResult
Dim jss As New JavaScriptSerializer()
Try
result = jss.Deserialize(json_string, GetType(SearchResult))
Catch ex As Exception
result = Nothing
Debug.WriteLine("Failed to deserialize " & json_string)
Debug.WriteLine(ex.Message)
End Try
Return result
End Function
End Class
And the API Error Class
<Serializable> _
Public Class APIError
Public Property error_fields() As String
End Class
I have tried making this a list and an array. I am continually getting an exception message that the Class is not supported for deserialization of an array.
I would prefer to use the JSS for deserializing and serializing as I have a difficult time selling my boss on third-party libraries.
Where am I going wrong? Thanks!