VB.Net反序列化包含字符串数组而不是键值对的JSON对象

时间:2014-06-03 19:24:36

标签: json vb.net serialization

我正在使用返回JSON的API。到目前为止,使用JavaScriptSerializer中的System.Web.Script.Serialization类,所有序列化和反序列化都很简单。有一个端点可以返回错误,并且不在其错误列表中使用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

我试过把它变成一个列表和一个数组。我不断收到类is not supported for deserialization of an array.

的异常消息

我更喜欢使用JSS进行反序列化和序列化,因为我很难在第三方库上销售我的老板。

我哪里错了?谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个深度嵌套的数组。尝试这样的事情(未经测试的VB格式):

<Serializable> _
Public Class SearchResult

    Public Property status As String    
    Public Property errors As List(Of List(Of string()))

    Public Sub New()
        errors = New List(Of List(Of string()))
        errors.Add(new List(Of string()))
    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

我测试的原始C#就是这个(它确实有效):

namespace JavaScriptSerializerApp
{
    class Program
    {
        static void Main(string[] args)
        {

            SearchResult sr = new SearchResult();
            sr.status = "failed";
            sr.errors[0].Add(new string[] { "base", "error details 1" });
            sr.errors[0].Add(new string[] { "base", "error details 2" });
            sr.errors[0].Add(new string[] { "base", "error details 3" });

            JavaScriptSerializer jss = new JavaScriptSerializer();

            string output1 = jss.Serialize(sr);

            string json = "{\"status\": \"failed\",\"errors\": [[[\"base\", \"error details 1\"],[\"base\", \"error details 2\"],[\"base\", \"error details 3\"]]]}";
            SearchResult sr2 = (SearchResult)jss.Deserialize(json, typeof(SearchResult));
            string output2 = jss.Serialize(sr2);

            Console.WriteLine((output1 == output2).ToString());

        }
    }

    [Serializable()]
    public class SearchResult
    {
        public SearchResult()
        {
            errors = new List<List<string[]>>();
            errors.Add(new List<string[]>());

        }

        public string status { get; set; }
        public List<List<string[]>> errors { get; set; }
    }

}