JsonConvert.DeserializeObject返回缺少的值

时间:2017-06-28 08:17:41

标签: asp.net vb.net

我正在尝试通过AJAX将数据推送到我的ASP.NET Web服务,该服务将数据插入到我的数据库中。但是,我在循环访问反序列化列表时遇到了问题。

这是我的班级:

       Public Class TimesheetDetails
    Inherits System.Web.Services.WebService

    Public Property TimesheetID() As String
        Get
            Return m_TimesheetID
        End Get
        Set
            m_TimesheetID = Value
        End Set
    End Property
    Private m_TimesheetID As String
    Public Property LineNumber() As String
        Get
            Return m_LineNumber
        End Get
        Set
            m_LineNumber = Value
        End Set
    End Property
    Private m_LineNumber As String
    Public Property ProjectCode() As String
        Get
            Return m_ProjectCode
        End Get
        Set
            m_ProjectCode = Value
        End Set
    End Property
    Private m_ProjectCode As String
    Public Property Comments() As String
        Get
            Return m_Comments
        End Get
        Set
            m_Comments = Value
        End Set
    End Property
    Private m_Comments As String
    Public Property SAT() As Decimal
        Get
            Return m_SAT
        End Get
        Set
            m_SAT = Value
        End Set
    End Property
    Private m_SAT As Decimal
    Public Property SUN() As Decimal
        Get
            Return m_SUN
        End Get
        Set
            m_SUN = Value
        End Set
    End Property
    Private m_SUN As Decimal
    Public Property MON() As Decimal
        Get
            Return m_MON
        End Get
        Set
            m_MON = Value
        End Set
    End Property
    Private m_MON As Decimal
    Public Property TUE() As Decimal
        Get
            Return m_TUE
        End Get
        Set
            m_TUE = Value
        End Set
    End Property
    Private m_TUE As Decimal
    Public Property WED() As Decimal
        Get
            Return m_WED
        End Get
        Set
            m_WED = Value
        End Set
    End Property
    Private m_WED As Decimal
    Public Property THU() As Decimal
        Get
            Return m_THU
        End Get
        Set
            m_THU = Value
        End Set
    End Property
    Private m_THU As Decimal
    Public Property FRI() As Decimal
        Get
            Return m_FRI
        End Get
        Set
            m_FRI = Value
        End Set
    End Property
    Private m_FRI As Decimal
    Public Property TOTAL() As Decimal
        Get
            Return m_TOTAL
        End Get
        Set
            m_TOTAL = Value
        End Set
    End Property
    Private m_TOTAL As Decimal

End Class

和方法

<WebMethod()>
     Public Function SaveData(empdata) As String
    'WebMethod to Save the data  

    Dim serializeData = JsonConvert.DeserializeObject(Of List(Of TimesheetDetails))(empdata)

    Dim ids As String = ""

    For Each obj As Object In serializeData

        If obj.LineNumber = "0" Then

        Else
            Dim timesheetid As String = obj.TimesheetID
            Dim linenumber As String = obj.LineNumber
            Dim projectcode As String = obj.ProjectCode
            Dim comments As String = obj.Comments
            Dim monday As String = obj.MON
            Dim tuesday As String = obj.TUE
            Dim wednesday As String = obj.WED
            Dim thursday As String = obj.THU
            Dim friday As String = obj.FRI
            Dim saturday As String = obj.SAT
            Dim sunday As String = obj.SUN
            Dim total As String = obj.TOTAL
            Dim noteid As String = "No id found"

            Debug.WriteLine(linenumber + " | " + projectcode + ", " + monday + tuesday + wednesday + thursday + friday + saturday + sunday)


        End If

    Next

    Return Nothing

End Function

数据从AJAX帖子完整到达,但是当数据被反序列化时,我的一些值保持不变,但其他值变为null或0。

以下是成功发布的之前值:

[
 {
  "timesheetid":"86",
  "linenumber":0
  },
  {
  "timesheetid":"86",
  "linenumber":1,
  "projectcode":"12988",
  "comments":" test comment",
  "monday":"7.5",
  "tuesday":"7.5",
  "wednesday":"7.5",
  "thursday":"7.5",
  "friday":"7",
  "saturday":"7.5",
  "sunday":"7.5",
  "total":"52"
  }
 ]

以下是数据反序列化后的值:

[
 {
  "timesheetid":"86",
  "linenumber":0
  },
  {
  "timesheetid":"86",
  "linenumber":1,
  "projectcode":"12988",
  "comments":" test comment",
  "monday":"0",
  "tuesday":"0",
  "wednesday":"0",
  "thursday":"0",
  "friday":"0",
  "saturday":"0",
  "sunday":"0",
  "total":"52"
  }
 ]

1 个答案:

答案 0 :(得分:1)

我认为问题是 TimesheetDetails 类的某些属性与发布的对象属性不同。例如,发布对象中的属性名称为 monday ,而在 TimesheetDetails 类中,其名称为 MON