我在从MVC填充FullCalendar方面遇到了困难,并希望对此事有所帮助。
我的控制器有以下代码:
Function GetEvents(ByVal [start] As Double, ByVal [end] As Double) As JsonResult
Dim sqlConnection As New SqlClient.SqlConnection
sqlConnection.ConnectionString = My.Settings.sqlConnection
Dim sqlCommand As New SqlClient.SqlCommand
sqlCommand.CommandText = "SELECT tripID AS ID, tripName AS Title, DATEDIFF(s, '1970-01-01 00:00:00', dateStart) AS [Start], DATEDIFF(s, '1970-01-01 00:00:00', dateEnd) AS [End] FROM tblTrip WHERE userID=18 AND DateStart IS NOT NULL"
sqlCommand.Connection = sqlConnection
Dim ds As New DataSet
Dim da As New SqlClient.SqlDataAdapter(sqlCommand)
da.Fill(ds, "Meetings")
sqlConnection.Close()
Dim meetings = From c In ds.Tables("Meetings") Select {c.Item("ID"), c.Item("Title"), "False", c.Item("Start"), c.Item("End")}
Return Json(meetings.ToArray(), JsonRequestBehavior.AllowGet)
End Function
这确实运行正确,但返回的格式为:
[[25,“South America 2008”,“False”,1203033600,1227657600],[48,“Levant 2009”,“False”,1231804800,1233619200],[49,“South America 2009”,“False “,1235433600,1237420800],[50,”Italy 2009“,”False“,1241049600,1256083200],[189,”Levant 2010a“,”False“,1265414400,1267574400],[195,”Levant 2010a“,” False“,1262736000,1262736000],[208,”Levant 2010a“,”False“,1264982400,1267574400],[209,”Levant 2010a“,”False“,1264982400,1265587200],[210,”Levant 2010“, “False”,1264982400,1266969600],[211,“Levant 2010 b”,“False”,1267056000,1267574400],[213,“South America 2010a”,“False”,1268438400,1269648000],[214,“Levant 2010 c“,”False“,1266364800,1264118400],[215,”South America 2010a“,”False“,1268611200,1269648000],[217,”South America 2010“,”False“,1268611200,1269561600],[ 218,“South America 2010 b”,“False”,1268956800,1269388800],[227,“levant 2010 b”,“False”,1265846400,1266192000]]
这与我在帖子上看到的完全不同:jQuery FullCalendar JSON date issue
(注意缺少标签信息和花括号)
有人可以向我解释一下我可能做错了什么以及为什么我的输出格式不正确。
TIA
答案 0 :(得分:0)
您不能只在对象上使用JsonSerializer - 正如您所看到的,它不是FullCalendar所需的正确格式。
您需要提供自己的序列化程序(从c#转换):
Public Class Meeting
Public ID As Integer
Public Title As Integer
Public Start As DateTime
Public [End] As DateTime
Public AllDay As Boolean
End Class
Public Class MeetingJavaScriptConverter
Inherits JavaScriptConverter
Private Shared ReadOnly _supportedTypes As Type() = New Type(-1) {GetType(Meeting)}
Public Overloads Overrides ReadOnly Property SupportedTypes() As IEnumerable(Of Type)
Get
Return _supportedTypes
End Get
End Property
Public Overloads Overrides Function Serialize(ByVal obj As Object, ByVal serializer As JavaScriptSerializer) As IDictionary(Of String, Object)
Dim meeting = TryCast(obj, Meeting)
If meeting IsNot Nothing Then
Dim dictionary = New Dictionary(Of String, Object)()
dictionary.Add("id", meeting.ID)
dictionary.Add("title", meeting.Title)
dictionary.Add("start", meeting.Start.ToJson())
dictionary.Add("end", meeting.[End].ToJson())
dictionary.Add("allDay", If(meeting.AllDay, "true", "false"))
Return dictionary
End If
Return New Dictionary(Of String, Object)()
End Function
Public Overloads Overrides Function Deserialize(ByVal dictionary As IDictionary(Of String, Object), ByVal type As Type, ByVal serializer As JavaScriptSerializer) As Object
Throw New NotImplementedException()
End Function
End Class
Public Module DateTimeExtensionMethods
Private Sub New()
End Sub
<System.Runtime.CompilerServices.Extension> _
Public Function ToJson(ByVal dateTime As DateTime) As String
Return dateTime.ToString("s")
End Function
End Module
一旦您从查询中填充了Meeting
列表,就可以按照以下方式使用它:
Dim serializer As New JavaScriptSerializer()
serializer.RegisterConverters(New () {New MeetingJavaScriptConverter()})
Dim jsonresult As String = serializer.Serialize(meetings.ToArray())
(使用:http://www.developerfusion.com/tools/convert/csharp-to-vb/转换)
答案 1 :(得分:0)
代码的轻微问题(可能是转换)......
此:
Private Shared ReadOnly _supportedTypes As Type() = New () {GetType(Meeting)}
Public Overloads Overrides ReadOnly Property SupportedTypes() As IEnumerable(Of Type)
Get
Return _supportedTypes
End Get
End Property
在VB.Net中不可接受,在“= New()”上显示错误
正确的代码是什么?我尝试过简单地使用“= New Meeting”,但得到“类型'MVC.Meeting'的错误,无法转换为'System.Type'的1维数组。”
答案 2 :(得分:0)
FAO CodeSleuth(以及其他需要此事的人)。
感谢你,并帮助我“理解”这个问题,我现在已经开始工作了。
我自己的代码如下:
Function GetEvents(ByVal [start] As Double, ByVal [end] As Double) As JsonResult
Dim sqlConnection As New SqlClient.SqlConnection
sqlConnection.ConnectionString = My.Settings.sqlConnection
Dim sqlCommand As New SqlClient.SqlCommand
sqlCommand.CommandText = "SELECT tripID AS ID, tripName AS Title, dateStart AS [Start], dateEnd AS [End] FROM tblTrip WHERE userID=18 AND DateStart IS NOT NULL"
sqlCommand.Connection = sqlConnection
Dim ds As New DataSet
Dim da As New SqlClient.SqlDataAdapter(sqlCommand)
da.Fill(ds, "Meetings")
sqlConnection.Close()
Dim meetings = From c In ds.Tables("Meetings") Select {c.Item("ID"), c.Item("Title"), "False", c.Item("Start"), c.Item("End"), "False"}
Dim meetingsArray As New ArrayList()
For Each dr As DataRow In ds.Tables("Meetings").Rows
Dim m As New Meeting
With m
.AllDay = False
.End = CDate(dr.Item("End")).ToJson()
.ID = dr.Item("ID")
.Start = CDate(dr.Item("Start")).ToJson()
.Title = dr.Item("Title")
End With
meetingsArray.Add(m)
Next
Return Json(meetingsArray, JsonRequestBehavior.AllowGet)
End Function
和以下类定义:
Public Class Meeting
Public id As Integer
Public title As String
Public start As String
Public [end] As String
Public allDay As Boolean
结束班
以及以下功能,再次感谢您:
Public Module DateTimeExtensionMethods
Sub New()
End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Function ToJson(ByVal dateTime As DateTime) As String
Return dateTime.ToString("s")
End Function
结束模块
完美无缺,虽然我需要稍微调整一下以从Linq结果中读取(或者只是将SQL命令更改为过滤)。
再一次,非常感谢帮助我理解这个问题,我坚信一个人需要理解,现在只需复制代码,你的帮助已经完成了。
杰森
答案 3 :(得分:0)
很抱歉我的速度很慢,但在我之前的短暂检查时,我错过了你的评论 这是我的答案的原始代码:
public class Meeting
{
public int ID;
public int Title;
public DateTime Start;
public DateTime End;
public bool AllDay;
}
public class MeetingJavaScriptConverter : JavaScriptConverter
{
private static readonly Type[] _supportedTypes = new[] { typeof(Meeting) };
public override IEnumerable<Type> SupportedTypes
{
get { return _supportedTypes; }
}
public override IDictionary<string, object> Serialize(
object obj, JavaScriptSerializer serializer)
{
var meeting = obj as Meeting;
if (meeting != null)
{
var dictionary = new Dictionary<string, object>();
dictionary.Add("id", meeting.ID);
dictionary.Add("title", meeting.Title);
dictionary.Add("start", meeting.Start.ToJson());
dictionary.Add("end", meeting.End.ToJson());
dictionary.Add("allDay", meeting.AllDay ? "true" : "false");
return dictionary;
}
return new Dictionary<string, object>();
}
public override object Deserialize(IDictionary<string, object> dictionary,
Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
public static class DateTimeExtensionMethods
{
public static string ToJson(this DateTime dateTime)
{
return dateTime.ToString("s");
}
}
用法(放在普通ASP.NET网站的Page_Load
中):
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new MeetingJavaScriptConverter() });
string jsonresult = serializer.Serialize(meetings.ToArray());
Response.Clear();
Response.ContentType = "text/javascript";
Response.Write(jsonresult);
Response.End();
我粘贴了额外的代码,显示如何使Page_Load
像http处理程序一样工作,因为这就是我使用它的方式。
显然你不必使用我写的扩展方法(因为这只是我写的东西,因为我使用了许多其他不同的Json序列化程序,这对它很有用)。
所以你去吧。这就是我的所有代码,对我来说就像一个魅力。祝好运! :)