【问题标题】:How to format asp.net web services (ASMX) to look like the mentioned example?如何格式化 asp.net web 服务 (ASMX) 看起来像提到的例子?
【发布时间】:2015-04-01 13:17:23
【问题描述】:

我正在尝试将我的 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"
    }
]

我的班级文件

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

【问题讨论】:

  • This answer 讨论了类似的问题。最简单的解决方法是发送 ISO 日期。
  • @MikeSmithDev 谢谢,但该示例是格式化 insde jQuery 脚本,我必须更改我的“asmx”文件中的格式。这是我在 asmx 中更改时出现的错误:System.InvalidCastException:从字符串“2015-03-23T08:15:00”转换输入 'Long'无效。 ---> System.FormatException:输入字符串的格式不正确。我在 FullCalendar.js 中调用我的 asmx,如下所示: events: "calendar.asmx/EventList",
  • 正如我所提到的,最简单的解决方法是发送 ISO 日期。日历脚本中的格式只是问题的一个演示。答案中包含指向另一个 SO 帖子的链接,该帖子显示了如何完成 ISO 日期格式。
  • @MikeSmithDev,我在尝试发生这种情况时发现它: System.InvalidCastException: Conversion from string "2015-03-23T08:15:00.0000000"输入 'Long'无效。 ---> System.FormatException: 输入字符串格式不正确。
  • 数据没问题。问题是它是 XML 而不是 JSON。 This 可能会告诉您如何返回 JSON。如果它是 C# 设置,也许@MikeSmithDev 可以提供帮助。如果不是,我需要查看你初始化 FC 的代码。

标签: c# asp.net json web-services fullcalendar


【解决方案1】:

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

再次特别感谢slicedtoadMikeSmithDev

我的班级文件

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"
    }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多