JsonConvert.SerializeObject() 未序列化列表(T),其中 T 是我的自定义 Class

JsonConvert.SerializeObject() not serializing a List(of T) where T is a custom Class of mine

我正在尝试将报价项目数据格式化为 JSON 对象以便稍后保存到 MySQL。以下代码生成 JSONObject,它返回一个空的 JSON 对象,如下所示:[{}]、即使 Quote_Items_Info_Data 不是空的.没有错误被触发! 为什么 JsonConvert 没有序列化?

Friend Class JSON_Quote_Item
    Friend Property ItemNumber As String
    Friend Property ItemDescription As String
    Friend Property ItemQty As String
    Friend Property ItemPrice As String
    Public Sub New()
    End Sub
    Public Sub New(ByVal _ItemNumber As String, ByVal _ItemDescription As String, ByVal _itemQty As String, ByVal _itemPrice As String)
        ItemNumber = _ItemNumber.Trim.Replace(vbCrLf, "\r\n").Replace(Chr(34), "\" & Chr(34)).Replace("'", "\'")
        ItemDescription = _ItemDescription.Trim.Replace(vbCrLf, "\r\n").Replace(Chr(34), "\" & Chr(34)).Replace("'", "\'")
        ItemQty = _itemQty.Trim
        ItemPrice = _itemPrice.Trim
    End Sub
End Class
'
'
''' <summary>
''' Convert all Quote items to a JSON object on the form and save it to the server DB.
''' senderFRM is the Quote Creator/Editor. Following an example from: https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
''' and https://www.newtonsoft.com/json
''' </summary>
''' <param name="senderFRM"></param>
''' <returns></returns>
Friend Function SaveAllQuoteItems(senderFRM As frmQuotesCreatorEditor) As Boolean
    Try
        'make sure I will not run into an exception!
        If senderFRM IsNot Nothing AndAlso String.IsNullOrEmpty(senderFRM.txtQuoteNumber.Text.Trim) = False Then
            Dim QuoteNumber As String = senderFRM.txtQuoteNumber.Text.Trim
            'Holds a list of the custom class JSON_Quote_Item as T and contain all the form quote items data.
            Dim Quote_Items_Info_Data As List(Of JSON_Quote_Item) = New List(Of JSON_Quote_Item)
            '

            '
            'Go thru all FlowLayout controls in the form with the finality of finding the Quote items data and put this into a JSON (serialized) string.
            For Each myPanel As Panel In senderFRM.flpQuoteItems.Controls.OfType(Of Panel)
                Dim myElementHost As ElementHost = myPanel.Controls.OfType(Of ElementHost).FirstOrDefault
                Dim MyUserControl_QuoteItem As UserControl_QuoteItem = TryCast(myElementHost.Child, UserControl_QuoteItem)
                '
                Dim myQuoteItemNumber As String = MyUserControl_QuoteItem.txtItemNumber.Text
                Dim myQuoteItemDescription As String = MyUserControl_QuoteItem.txtItemDescription.Text
                Dim myQuoteItemQty As String = MyUserControl_QuoteItem.txtItemQty.Text
                Dim MyQuoteItemPrice As String = MyUserControl_QuoteItem.txtItemListedCustomerPrice.Text
                '
                'Perhaps not necessary but here to check that all parameters contain data.
                If String.IsNullOrEmpty(myQuoteItemNumber) = False And String.IsNullOrEmpty(myQuoteItemDescription) = False And
                    String.IsNullOrEmpty(myQuoteItemQty) = False And String.IsNullOrEmpty(MyQuoteItemPrice) = False Then
                    'add the data to the list of UserControl_QuoteItem
                    Quote_Items_Info_Data.Add(New JSON_Quote_Item(myQuoteItemNumber, myQuoteItemDescription, MyQuoteItemPrice, myQuoteItemQty))
                End If
            Next
            '
            Dim JSONObject As Object = DBNull.Value
            If Quote_Items_Info_Data IsNot Nothing AndAlso Quote_Items_Info_Data.Count > 0 Then
                '
                'NuGet - Newtonsoft.Json.JsonConvert
                'Return as string, but will keep as an object to be able to use it on the parameterization of the SQL query later!
                JSONObject = JsonConvert.SerializeObject(Quote_Items_Info_Data)
            End If
            '
            Console.WriteLine(JSONObject.ToString)
            'JSONObject is returning an empty JSON Object returning this: [{}] ,  even though Quote_Items_Info_Data was not empty.
            'Why on earth is JsonConvert is not serializing?
            '
            'Code to save to the quote column in the database code goes here if the above ever Works!
        End If
        Return True
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
    Return False
End Function

找到解决方案! 问题在于新的短形式的属性(a one-liner)。 更改:

 Friend Property ItemNumber As String
 Friend Property ItemDescription As String
 Friend Property ItemQty As String
 Friend Property ItemPrice As String

为此:

        Private itemNumber As String
        Public Property _itemNumber() As String
            Get
                Return itemNumber
            End Get
            Set(ByVal value As String)
                itemNumber = value
            End Set
        End Property
        '
        Private ItemDescription As String
        Public Property _ItemDescription() As String
            Get
                Return ItemDescription
            End Get
            Set(ByVal value As String)
                ItemDescription = value
            End Set
        End Property

        Private ItemQty As String
        Public Property _ItemQty() As String
            Get
                Return ItemQty
            End Get
            Set(ByVal value As String)
                ItemQty = value
            End Set
        End Property
        Private ItemPrice As String
        Public Property _ItemPrice() As String
            Get
                Return ItemPrice
            End Get
            Set(ByVal value As String)
                ItemPrice = value
            End Set
        End Property

一切都成功了!!缩写形式中没有 getter?

问题确实是声明访问级别 FRIEND.So: 这个:

    Public Property ItemNumber As String
    Public Property ItemDescription As String
    Public Property ItemQty As String
    Public Property ItemPrice As String

Do work Too 而且干净多了! 谢谢,@Jimi,有时人会失明!