.NET Web API:使用属性作为List(Of对象)反序列化JSON对象

时间:2015-04-08 11:55:32

标签: json asp.net-web-api

想象一下,我创建了一个像这样的......类......

Public Class Filter

    Public Enum enuOperator
        [EqualTo] = 0
        [Like] = 1
        [In] = 2
        [StartsWith] = 3
        [EndsWith] = 4
        [NotNull] = 5
        [Null] = 6
    End Enum

    Public Class FilterItem

        Public Property [Field] As String
        Public Property [Operator] As enuOperator
        Public Property [Value] As Object

        Public Sub New(filterField As String, filterOperator As enuOperator, filterValue As Object)
            With Me
                .Field = filterField
                .Operator = filterOperator
                .Value = filterValue
            End With
        End Sub

        Public Sub New(filterField As String, filterValue As Object)
            Me.New(filterField, enuOperator.EqualTo, filterValue)
        End Sub

    End Class

    Public Property Filters As List(Of FilterItem)

    Public Sub New()
    End Sub

    Public Sub New(filterItems As List(Of FilterItem))
        Me.Filters = filterItems
    End Sub

End Class

如您所见,此类“Filter”包含一个属性“Filters”,它基本上是一个“FilterItem”对象的数组(实际上是List)。

现在,我可以使用JSON将此“Filter”类的实例发送到我的ASP.NET Web API,从而产生这种表示法:

{"Filters":[{"Field":"PrTy_Id","Operator":0,"Value":3}]}

但是,在接收方(在Web API本身中),尽管它读出完全相同(作为JSON格式的字符串),并且它正确地转换为“Filter”对象,但具有属性“Filters”作为“FilterItem”对象的空列表!

显然我的问题是,如何读出“过滤器”属性,并将其正确转换为现有的对象列表?

1 个答案:

答案 0 :(得分:0)

当我手动将我的JSON字符串反序列化为object:

时,我自己想出来了

显然,我只是在" FilterItem"中错过了(无参数) New()构造函数。类。