JSon使用数据表内部序列化复杂对象

时间:2017-10-18 13:06:47

标签: json vb.net serialization datatable

我有这样的对象:

Public Class ClasseProva
Inherits RM_Base

Public Sub New()
    Me.NomeTabella = "ClasseProva"
End Sub

Private m_AZI_ID As Integer
Public Property AZI_ID() As Integer
    Get
        Return m_AZI_ID
    End Get
    Set(ByVal value As Integer)
        m_AZI_ID = value
    End Set
End Property

Private m_dtPROGETTI As DataTable

Public Function ProgettiRO() As DataTable
    Return m_dtPROGETTI
End Function

Public Sub ProgettiWO(value As DataTable)
    m_dtPROGETTI = value
End Sub

End Class

因此,如果在我的对象中没有数据表,我知道如何使用Json序列化并使用序列化对象作为cookie。所以我这样做了

public ClasseProva CookieClasseProva
{
    get
    {
        try
        {
            string myObjectJson = Request.Cookies["CookieClasseProva"].Value;
            return JsonConvert.DeserializeObject<ClasseProva>(myObjectJson);
        }
        catch { return null; }
    }
    set
    {
        string myObjectJson = JsonConvert.SerializeObject(value);
        var cookie = new HttpCookie("CookieClasseProva", myObjectJson)
        {
            Expires = DateTime.Now.AddYears(1)
        };
        Response.Cookies.Add(cookie);
    }
}

但是在添加数据表之后,序列化函数没有序列化数据表。

编辑:

我不知道如何附加项目。但现在我正在复制我的BL项目的课程

    Public Class Class1
        Private m_intValue As Integer
        Public Property MyIntValue() As Integer
            Get
                Return m_intValue
            End Get
            Set(ByVal value As Integer)
                m_intValue = value
            End Set
        End Property

        Private m_dtValue As DataTable
        Public Property MyDatatable() As DataTable
            Get
                Return m_dtValue
            End Get

            Set(value As DataTable)
                m_dtValue = value
            End Set
        End Property

    End Class

和简单页面的代码部分

    Imports Newtonsoft.Json
    Imports DatatableSerializationBL

    Public Class _Default
        Inherits Page

        Public Property CookieUtente() As Class1
            Get
                Try
                    Dim myObjectJson As String = Request.Cookies("CookieUtente").Value
                    Return JsonConvert.DeserializeObject(Of Class1)(myObjectJson)
                Catch
                    Return Nothing
                End Try
            End Get
            Set(value As Class1)
                Dim myObjectJson As String = JsonConvert.SerializeObject(value)
                Dim cookie = New HttpCookie("CookieUtente", myObjectJson)
                Response.Cookies.Add(cookie)
            End Set
        End Property

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim c As New Class1
            c.MyIntValue = 1
            c.MyDatatable = New DataTable
            c.MyDatatable.NewRow()
            CookieUtente = c
            Console.WriteLine(CookieUtente.MyDatatable.Rows.Count)
        End Sub

    End Class

所以在设计页面中,我打印rows.count值并打印0

ROWS : <%= CookieUtente.MyDatatable.Rows.Count%>

0 个答案:

没有答案
相关问题