0

我有一个包含销售数据的 Sale 类,然后我在 Sale 类中有一个属性,它是 SaleItem 类的列表。

Sale
-date
-SaleItems <-- List(Of SaleItem)
-total

SaleItem 类

SaleItem
-productID
-amt
-qty

当我转换为 Json 字符串并调试打印输出时。SaleItems 没有编码,只是丢失了。

我做错了什么,或者这可能是一个错误?

编辑

销售类

Public Class Sale
    Private _Paid As Decimal
    Private _ListSaleItems As List(Of SaleItem)

    Public Sub New()
        ClearSaleItem()
    End Sub

    Public ReadOnly Property SaleItems() As List(Of SaleItem)
        Get
            Return _ListSaleItems
        End Get
    End Property


    Public Property Paid() As Decimal
        Get
            Return _Paid
        End Get
        Set
            _Paid = value
        End Set
    End Property


    Public Sub ClearSaleItem()
        _ListSaleItems = New List(Of SaleItem)()
    End Sub


    Public Function AddSaleItem(value As SaleItem) As Decimal
        If value Is Nothing Then
            Throw New ApplicationException("SaleItem cannot be empty")
        End If
        _ListSaleItems.Add(value)
    End Function
End Class

Saleitem类

Public Class SaleItem
    Private _ID As Int32
    Private _Quantity As Int32
    Private _SellPrice As Decimal


    Public Sub New()
        _Quantity = 0
        _SellPrice = 0
    End Sub

    Public Property SellPrice() As Decimal
        Get
            Return _SellPrice
        End Get
        Set
            _SellPrice = value
        End Set
    End Property

    Public Property Quantity() As Int32
        Get
            Return _Quantity
        End Get
        Set
            _Quantity = value
        End Set
    End Property

End Class

我用来转换的代码是

sockClient.Send(fastJSON.JSON.ToJSON(ObjSales))
4

2 回答 2

0

好的,这是我的工作代码(在 C# 而不是 VB 中)证明这不是来自 lib 的错误:

namespace TestFastJson
{
    internal class Program
    {
            private static void Main()
            {
                    var sale = new Sale
                    {
                        SaleItems = new List<SaleItem>
                        {
                            new SaleItem(),
                            new SaleItem()
                        }
                    };

                    string jsonText = fastJSON.JSON.ToJSON(sale);

                    var newobj = fastJSON.JSON.ToObject<Sale>(jsonText);

                    Console.WriteLine(newobj.SaleItems.Count);
            }
    }

    public class Sale
    {
            public DateTime Date { get; set; }
            public List<SaleItem> SaleItems { get; set; }
            public int Total { get; set; }
    }

    public class SaleItem
    {
            public string ProductId { get; set; }
            public int Amount { get; set; }
            public string Quality { get; set; }
    }
}

输出

2

现在为您的代码。为什么AddSaleItem()返回一个Decimal?并且SaleItems丢失是因为SaleItems设置为private。切换到public这里。如果 setter 不可访问,lib 如何设置值?

您的代码具有有效输出:

    Friend Class Program
        Private Shared Sub Main()
            Dim sale = New Sale()
            sale.AddSaleItem(New SaleItem())
            sale.AddSaleItem(New SaleItem())

            Dim jsonText As String = fastJSON.JSON.ToJSON(sale)

            Console.WriteLine(jsonText)

            Dim newobj = fastJSON.JSON.ToObject(Of Sale)(jsonText)
            Console.WriteLine(newobj.SaleItems.Count)
        End Sub
    End Class

    Public Class SaleItem
        Private _ID As Int32

        Public Sub New()
            Me.Quantity = 0
            Me.SellPrice = 0
        End Sub

        Public Property SellPrice() As Decimal
            Get
                Return m_SellPrice
            End Get
            Set
                m_SellPrice = Value
            End Set
        End Property
        Private m_SellPrice As Decimal
        Public Property Quantity() As Integer
            Get
                Return m_Quantity
            End Get
            Set
                m_Quantity = Value
            End Set
        End Property
        Private m_Quantity As Integer
    End Class

    Public Class Sale
        Public listSaleItems As List(Of SaleItem)
        Public Sub New()
            Me.ClearSaleItem()
        End Sub

        Public ReadOnly Property SaleItems() As List(Of SaleItem)
            Get
                Return Me.listSaleItems
            End Get
        End Property

        Public Property Paid() As Decimal
            Get
                Return m_Paid
            End Get
            Set
                m_Paid = Value
            End Set
        End Property
        Private m_Paid As Decimal

        Public Sub ClearSaleItem()
            Me.listSaleItems = New List(Of SaleItem)()
        End Sub

        Public Sub AddSaleItem(value As SaleItem)
            If value Is Nothing Then
                Throw New ApplicationException("SaleItem cannot be empty")
            End If
            Me.listSaleItems.Add(value)
        End Sub
    End Class

输出

2
于 2015-09-27T06:10:09.020 回答
0

感谢您的贡献,恰好dll编码器给了我解决方案

我必须将 Sale 的 Saleitems 属性从 Readonly 更改为 Read/Write 属性

Public Property SaleItems() As List(Of SaleItem)
            Get
                Return Me.listSaleItems
            End Get
Set(value as List(Of SaleItem))
                listSaleItems = value 
            End Get
        End Property

谢谢

于 2015-09-29T07:10:26.707 回答