ServiceStack - 返回类时清空json

时间:2013-07-04 11:59:15

标签: asp.net vb.net web-services .net-4.0 servicestack

在将类连接到JSON时,ServiceStack有一个非常奇怪的问题 - 对象是空的,但XML工作正常。

发现一些建议,JSON序列化程序仅在属性公开时起作用,但正如您在下面看到的,我的属性是公共的。

请参阅下面的代码和截图。任何建议都非常感谢。

<小时/> 的 XML enter image description here
JSON enter image description here
 的 GetUsers.aspx.vb

Public Class GetUsersAD

        Public Property username As String
        Public Property fullname As String


        Public Function HandleRequest()

              Dim _users As New List (Of User)
             _users = GetUsersTest(_users)
              Return _users

        End Function

        Public Function GetUsersTest(_users As List (Of User)) As List ( Of User )

              Dim dt As New DataTable
             dt.Columns.Add( "username" )
             dt.Columns.Add( "fullname" )

              For x As Integer = 0 To 5

                     Dim newUserRow As DataRow = dt.NewRow()
                    newUserRow( "username" ) = "username-" & x & ""
                    newUserRow( "fullname" ) = "fullname-" & x & ""
                    dt.Rows.Add(newUserRow)

              Next

              For Each row As DataRow In dt.Rows

                     Dim _user As New User
                    _user.username = row( "username" )
                    _user.fullname = row( "fullname" )
                    _users.Add(_user)

              Next


              Return _users

        End Function

End Class

Public Class User

        Public username As String = ""
        Public fullname As String = ""


End Class

Public Class Users

        Public username As String = ""
        Public fullname As String = ""


End Class

WS.vb

Public Class WrapperGetUsers

    Implements IService(Of GetUsersAD)

    Public Property username As String
    Public Property fullname As String

    Public Function Execute(ByVal request As GetUsersAD) As Object Implements ServiceStack.ServiceHost.IService(Of GetUsersAD).Execute

        Return request.HandleRequest()

    End Function


End Class

1 个答案:

答案 0 :(得分:9)

默认情况下,ServiceStack只会在您的DTO中序列化公共属性,而不是公共字段。因此,您需要在username类中将fullnameproperty各自声明为User,或者将ServiceStack.Text配置为序列化字段(通过设置JsConfig.IncludePublicFields真实的。)

请注意,对于JSON序列化,使用ServiceStack.Text,而对于XML序列化,则使用BCL XML序列化程序。因此,XML与JSON序列化的配置和行为可以通过ServiceStack在这些方式上有所不同。