首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JsonConvert.SerializeObject()不序列化( T)列表,其中T是我的自定义类

JsonConvert.SerializeObject()不序列化( T)列表,其中T是我的自定义类
EN

Stack Overflow用户
提问于 2022-02-11 16:11:58
回答 2查看 263关注 0票数 1

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

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-11 16:40:37

问题确实在于声明访问级别是FRIEND.So:

代码语言:javascript
复制
    Public Property ItemNumber As String
    Public Property ItemDescription As String
    Public Property ItemQty As String
    Public Property ItemPrice As String

也要工作,而且要干净得多!“谢谢,”吉米说,“有时候人会瞎的!”

票数 0
EN

Stack Overflow用户

发布于 2022-02-11 16:28:16

找到解决办法了!问题在于,新的短属性形式(一条直线)。更改:

代码语言:javascript
复制
 Friend Property ItemNumber As String
 Friend Property ItemDescription As String
 Friend Property ItemQty As String
 Friend Property ItemPrice As String

对此:

代码语言:javascript
复制
        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

一切都成功了!!在短的形式里没有收获吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71083206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档