【问题标题】:VB.Net Deserialize JSON object that contains an array of strings instead of key value pairsVB.Net反序列化包含字符串数组而不是键值对的JSON对象
【发布时间】:2014-06-03 19:24:36
【问题描述】:

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


    【解决方案1】:

    这是一个深度嵌套的数组。试试这样的东西(未经测试的 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; }
        }
    
    }
    

    【讨论】:

    • 好吧,你明白了。我只是在尝试Public Property errors As List(Of List(Of List(Of String)))(哇,这太丑了)。你的结果是一个非无限的嵌套(或者看起来是无限的——调试器只是在连续运行)。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-06-11
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多