【发布时间】:2014-06-03 19:24:36
【问题描述】:
我正在使用返回 JSON 的 API。到目前为止,使用来自System.Web.Script.Serialization 的JavaScriptSerializer 类,所有的序列化和反序列化都很简单。有一个端点可以返回错误,并且在其错误列表中不使用 KVP(不知道为什么,违反了其余 api 的结构)。
我无法确定要反序列化的类结构。
示例返回:
{
"status": "failed",
"errors": [
[
["base", "error details 1"],
["base", "error details 2"],
["base", "error details 3"]
]
]
}
这是一个令人困惑的数据结构,因为其他所有内容都是配对的。但无论如何,我已经尝试使用数组和列表来处理错误。这是我的课程:
<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
还有 API 错误类
<Serializable> _
Public Class APIError
Public Property error_fields() As String
End Class
我已尝试将其设为列表和数组。我不断收到一条异常消息,指出 Class is not supported for deserialization of an array.
我更喜欢使用 JSS 进行反序列化和序列化,因为我很难将我的老板推销给第三方库。
我哪里错了?谢谢!
【问题讨论】:
标签: json vb.net serialization