FastJson编码器错误

时间:2015-09-22 17:55:50

标签: .net json fastjson

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

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

SaleItem类

SaleItem
-productID
-amt
-qty

当我转换为Json字符串并调试print到输出时。 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))

2 个答案:

答案 0 :(得分:0)

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

2

输出

AddSaleItem()

现在为你的代码。为什么Decimal会返回SaleItems? 由于SaleItems设置为private,因此缺少public。切换到 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 ,就在这里。如果无法访问setter,lib如何设置值?

您的代码具有有效输出:

2

输出

{{1}}

答案 1 :(得分:0)

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

我必须将Sale的Saleitems属性从Readonly更改为读/写属性

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

由于