c# - How to format asp.net web services (ASMX) to look like the mentioned example? -
i'm trying format web service result (json) mentioned example. current format fullcalendar not able display events.
if @ output see date format different example though tried not use (tounixtimespan) , have double quotes on every item such as, title,start,end , id.
how can rid of double quotes , format date , time?
i appreciate efforts in reaching solution 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 google', url: 'http://google.com/', start: '2014-11-28' } ]
my output looks 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 int32 public property id() int32 return m_id end set(byval value int32) m_id = value end set end property private m_start int64 public property start() int64 return m_start end set(byval value int64) m_start = value end set end property private m_end int64 public property [end]() int64 return m_end end set(byval value int64) m_end = value end set end property private m_title string public property title() string return m_title end set(byval value string) m_title = value end set end property 'private m_allday string 'public property allday() string ' ' return m_allday ' end ' set(byval value 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() string dim events new list(of calendardto) dim comm1 sqlcommand dim conn1 sqlconnection dim reader1 sqldatareader dim connectionstring1 string = configurationmanager.connectionstrings("caltest").connectionstring conn1 = new sqlconnection(connectionstring1) comm1 = new sqlcommand("select [id],[title] ,[description],[start] ,[end] ,[allday] [caltest].[dbo].[event]", conn1) conn1.open() reader1 = comm1.executereader() while reader1.read() dim value 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 new system.web.script.serialization.javascriptserializer return js.serialize(events) end function private function tounixtimespan(byval d datetime) int64 dim time 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 string) datetime dim time datetime = new datetime(1970, 1, 1, 0, 0, 0) return time.addseconds(s) end function end class
i decided share final codes of you.
once again special slicedtoad , mikesmithdev
my class file
public class calendardto private m_id string public property id() string return m_id end set(byval value string) m_id = value end set end property private m_title string public property title() string return m_title end set(byval value string) m_title = value end set end property private m_start string public property start() string return m_start end set(byval value string) m_start = value end set end property private m_end string public property [end]() string return m_end end set(byval value string) m_end = value end set end property private m_allday string public property allday() string return m_allday end set(byval value 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 ' allow web service called script, using asp.net ajax, uncomment 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 string, byval enddate string) string ' list hold events me.context.response.contenttype = "application/json; charset=utf-8" dim events new list(of calendardto) dim comm1 sqlcommand dim conn1 sqlconnection dim reader1 sqldatareader dim connectionstring1 string = configurationmanager.connectionstrings("caltest").connectionstring conn1 = new sqlconnection(connectionstring1) comm1 = new sqlcommand("select [id],[title] ,[description],[startdate_event] ,[enddate_event] ,[allday] [caltest].[dbo].[event]", conn1) conn1.open() reader1 = comm1.executereader() while reader1.read() dim value calendardto = new calendardto() dim newstartdate datetime = reader1("startdate_event").tostring() dim newenddate 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 new system.web.script.serialization.javascriptserializer() dim strjson 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" } ]
Comments
Post a Comment