我试图将报价项数据格式化为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
2条答案
按热度按时间mfpqipee1#
问题确实是声明访问级别是FRIEND而不是PUBLIC。
因此,将其改写为:
上面的工作,而且更干净!谢谢,@Jimi,有时候一个人会被蒙蔽!
ux6nzvsh2#
找到解决方案了!问题在于新的属性的简短形式(一行程序)。变更:
对此:
一切都成功了!在短格式中没有getter?