如何格式化 asp.net 网络服务 (ASMX) 使其看起来像上述示例?

How to format asp.net web services (ASMX) to look like the mentioned example?

我正在尝试格式化我的 Web 服务结果 (JSON) 以使其看起来像上述示例。 使用当前格式 FullCalendar 无法显示我的活动。

如果您查看我的输出,您会发现日期格式与示例不同,即使我尝试不使用 (ToUnixTimespan) 并且我在每个项目(例如标题、开始、结束和id.

如何去掉那些双引号并设置日期和时间的格式?

感谢您为解决我的问题所做的努力。

例子

 events: [
          {
            title: 'All Day Event',
            start: '2014-11-01'
          },
          {
            title: 'Long Event',
            start: '2014-11-07',
            end: '2014-11-10'
          },
          {
            id: 999,
            title: 'Repeating Event',
            start: '2014-11-09T16:00:00'
          },
          {
            id: 999,
            title: 'Repeating Event',
            start: '2014-11-16T16:00:00'
          },
          {
            title: 'Conference',
            start: '2014-11-11',
            end: '2014-11-13'
          },
          {
            title: 'Meeting',
            start: '2014-11-12T10:30:00',
            end: '2014-11-12T12:30:00'
          },
          {
            title: 'Lunch',
            start: '2014-11-12T12:00:00'
          },
          {
            title: 'Meeting',
            start: '2014-11-12T14:30:00'
          },
          {
            title: 'Happy Hour',
            start: '2014-11-12T17:30:00'
          },
          {
            title: 'Dinner',
            start: '2014-11-12T20:00:00'
          },
          {
            title: 'Birthday Party',
            start: '2014-11-13T07:00:00'
          },
          {
            title: 'Click for Google',
            url: 'http://google.com/',
            start: '2014-11-28'
          }
        ]

我的输出是这样的

[
    {
        "id": 10,
        "start": 1427094900,
        "end": 1427185800,
        "title": "new"
    },
    {
        "id": 11,
        "start": 1426978800,
        "end": 1427065200,
        "title": "hi"
    },
    {
        "id": 12,
        "start": 1427094000,
        "end": 1427181300,
        "title": "hi2"
    },
    {
        "id": 13,
        "start": 1427094900,
        "end": 1427100300,
        "title": "test"
    },
    {
        "id": 14,
        "start": 1427094000,
        "end": 1427184900,
        "title": "Al"
    },
    {
        "id": 15,
        "start": 1427698800,
        "end": 1427710500,
        "title": "CalTest"
    }
]

我的Class文件

Public Class CalendarDTO

    Private m_id As Int32
    Public Property id() As Int32
        Get
            Return m_id
        End Get
        Set(ByVal value As Int32)
            m_id = value
        End Set
    End Property

    Private m_Start As Int64
    Public Property start() As Int64
        Get
            Return m_Start
        End Get
        Set(ByVal value As Int64)
            m_Start = value
        End Set
    End Property

    Private m_End As Int64
    Public Property [end]() As Int64
        Get
            Return m_End
        End Get
        Set(ByVal value As Int64)
            m_End = value
        End Set
    End Property


    Private m_Title As String
    Public Property title() As String
        Get
            Return m_Title
        End Get
        Set(ByVal value As String)
            m_Title = value
        End Set
    End Property


    'Private m_allday As String
    'Public Property allDay() As String
    '    Get
    '        Return m_allday
    '    End Get
    '    Set(ByVal value As String)
    '        m_allday = value
    '    End Set
    'End Property
End Class

我的网络服务

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization


<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://someurl/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Calendar
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function EventList() As String


        Dim events As New List(Of CalendarDTO)

        Dim comm1 As SqlCommand
        Dim conn1 As SqlConnection
        Dim reader1 As SqlDataReader
        Dim connectionString1 As String = ConfigurationManager.ConnectionStrings("CalTest").ConnectionString
        conn1 = New SqlConnection(connectionString1)
        comm1 = New SqlCommand("SELECT [id],[title] ,[description],[start] ,[end] ,[allday] FROM [CalTest].[dbo].[event]", conn1)
        conn1.Open()

        reader1 = comm1.ExecuteReader()
        While reader1.Read()


            Dim value As CalendarDTO = New CalendarDTO()

            value.id = reader1("id").ToString()
            value.title = reader1("title").ToString()
            value.start = ToUnixTimespan(reader1("start").ToString())
            value.end = ToUnixTimespan(reader1("end").ToString())

            events.Add(value)

        End While

        reader1.Close()
        conn1.Close()

        Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
        Return js.Serialize(events)
    End Function


    Private Function ToUnixTimespan(ByVal d As DateTime) As Int64
        Dim time As New TimeSpan()
        time = d.ToUniversalTime().Subtract(New DateTime(1970, 1, 1, 0, 0, 0))

        Return CType(Math.Truncate(time.TotalSeconds), Int64)
    End Function

    Private Function FromUnixTimespan(ByVal s As String) As DateTime
        Dim time As DateTime = New DateTime(1970, 1, 1, 0, 0, 0)
        Return time.AddSeconds(s)
    End Function

End Class

我决定与大家分享我的最终代码。

再次特别感谢slicedtoad and MikeSmithDev

我的Class文件

Public Class CalendarDTO
    Private m_id As String
    Public Property id() As String
        Get
            Return m_id
        End Get
        Set(ByVal value As String)
            m_id = value
        End Set
    End Property

    Private m_Title As String
    Public Property title() As String
        Get
            Return m_Title
        End Get
        Set(ByVal value As String)
            m_Title = value
        End Set
    End Property

    Private m_Start As String
    Public Property start() As String
        Get
            Return m_Start
        End Get
        Set(ByVal value As String)
            m_Start = value
        End Set
    End Property

    Private m_End As String
    Public Property [end]() As String
        Get
            Return m_End
        End Get
        Set(ByVal value As String)
            m_End = value
        End Set
    End Property

    Private m_allday As String
    Public Property allDay() As String
        Get
            Return m_allday
        End Get
        Set(ByVal value As String)
            m_allday = value
        End Set
    End Property
End Class

我的网络方法 (ASMX)

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports System.Web.Script.Serialization
Imports System.ServiceModel.Web


' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://someurl/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Calendar
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Sub EventList()
        'Public Function EventList(ByVal startDate As String, ByVal endDate As String) As String
        ' List to hold events
        Me.Context.Response.ContentType = "application/json; charset=utf-8"

        Dim events As New List(Of CalendarDTO)

        Dim comm1 As SqlCommand
        Dim conn1 As SqlConnection
        Dim reader1 As SqlDataReader
        Dim connectionString1 As String = ConfigurationManager.ConnectionStrings("CalTest").ConnectionString
        conn1 = New SqlConnection(connectionString1)
        comm1 = New SqlCommand("SELECT [id],[title] ,[description],[startdate_event] ,[enddate_event] ,[allday] FROM [CalTest].[dbo].[event]", conn1)
        conn1.Open()


        reader1 = comm1.ExecuteReader()
        While reader1.Read()

            Dim value As CalendarDTO = New CalendarDTO()

            Dim newstartdate As DateTime = reader1("startdate_event").ToString()
            Dim newenddate As DateTime = reader1("enddate_event").ToString()

            value.id = reader1("id").ToString()
            value.title = reader1("title").ToString()
            value.start = newstartdate.ToString("s")
            value.end = newenddate.ToString("s")
            value.allDay = reader1("allday").ToString()

            events.Add(value)
        End While

        reader1.Close()
        conn1.Close()


        Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim strJSON As String = jss.Serialize(events)
        Context.Response.Write(jss.Serialize(events))

    End Sub
End Class

JSON输出

[
    {
        "id": "10",
        "title": "new",
        "start": "2015-03-23T08:15:00",
        "end": "2015-03-24T09:30:00",
        "allDay": ""
    },
    {
        "id": "11",
        "title": "hi",
        "start": "2015-03-22T00:00:00",
        "end": "2015-03-23T00:00:00",
        "allDay": "True"
    },
    {
        "id": "12",
        "title": "hi2",
        "start": "2015-03-23T08:00:00",
        "end": "2015-03-24T08:15:00",
        "allDay": "False"
    },
    {
        "id": "13",
        "title": "test",
        "start": "2015-03-23T08:15:00",
        "end": "2015-03-23T09:45:00",
        "allDay": "False"
    },
    {
        "id": "14",
        "title": "Kevin",
        "start": "2015-03-23T08:00:00",
        "end": "2015-03-24T09:15:00",
        "allDay": "False"
    },
    {
        "id": "15",
        "title": "Home test",
        "start": "2015-03-24T08:15:00",
        "end": "2015-03-25T10:15:00",
        "allDay": "False"
    }
]