问题
I'm trying to format my web service result (JSON) to look like the mentioned example. With this current format FullCalendar is not able to display my events.
If you look at my output you will see that the date format is different from the example even though I tried not to use (ToUnixTimespan) and I have double quotes on every item such as, title,start,end and id.
How can I get rid of those double quotes and format the date and time?
I appreciate your efforts in reaching a solution for my problem.
EXAMPLE
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'
}
]
My output looks like this
[
{
"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"
}
]
My Class file
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
My web service
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
回答1:
I decided to share my final codes with all of you.
Once again special thanks to slicedtoad and MikeSmithDev
My Class file
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
My Web Method (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 Output
[
{
"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"
}
]
来源:https://stackoverflow.com/questions/29391891/how-to-format-asp-net-web-services-asmx-to-look-like-the-mentioned-example